Refreshing a Google Sheets Filter with Apps Script

The following code allows for forcing the refresh of a Google Sheets filter to update the filtered data after making changes.


Usage Example

Here is an example of a Google Sheets filter that filters data whose reference contains -US-:

sheets filter refresh

If we now change the value of item 12, the filter results will not be automatically updated:

function example() {
  SpreadsheetApp.getActiveSheet().getRange('B13').setValue('O3WB7-US-1683');
}

To update the filter data (or create the filter if it does not yet exist or has been removed), execute the following function:

function filter() {
  // Source: https://www.sheets-pratique.com/en/codes/refresh-filter
  const sheet = SpreadsheetApp.getActiveSheet();
  const filter = sheet.getFilter() ?? sheet.getRange('A:C').createFilter();
  filter.setColumnFilterCriteria(2, SpreadsheetApp.newFilterCriteria()
    .whenTextContains('-US-')
    .build()
  );
}
Modify the columns as well as the parameters of setColumnFilterCriteria according to your filter (if you don’t know how to do this, you can record a macro while manually creating the filter to get an example).

The filter is then refreshed and in this case also displays item 12:

sheets filter force update apps script refresh