Coder14
Coder14

Reputation: 1277

Auto sort range, but only when there is a Filter View active

I have a Google spreadsheet which uses two views:

While in the Filter view, the user can update some cells which will change the sort order. I would like the sorting to immediately update.

I've found lots of scripts online to automatically sort on edit, but I only want this to happen when the Filter view is active.

I've found no methods to detect if a certain Filter view is active, and I haven't found anything returning the current sorting of a column either.

Any ideas on how I could do this?

Upvotes: 3

Views: 264

Answers (1)

Amit Agarwal
Amit Agarwal

Reputation: 11278

You can write a simple condition in the sort function that will test if the sheet filter is active. The isRowHiddenByFilter(rowPosition) method of the Sheet Class returns whether the given row is hidden by a filter.

function checkSheetFilter() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var rows = sheet.getRange(1,1, sheet.getLastRow(), 1).getValues().length;
  var isFilterActive = false;
  for (var r=0; r<rows && !isFilterActive; r++) {
    if (sheet.isRowHiddenByFilter(r+1)) {
      isFilterActive = true
    }
  }
  Logger.log(isFilterActive);
}

Upvotes: 3

Related Questions