Google Sheets Appscript on apply filter event

google sheetsgoogle-apps-scriptgoogle-apps-script-triggers

I want to execute a script as soon as I apply a filter to my Sheet. I can't find any event in the App script Docs that will allow me to do this, no onFilter event or anything obvious anyway. Does anyone know if this is possible, and if so, how to do it?

Best Answer

There is no simple trigger that runs when a filter is applied, but you can use an installable trigger to detect that. You can test an installable "on change" trigger like this:

function onChange(e) {
  console.log(JSON.stringify(e));
}

To make the code run when the spreadsheet is changed, click the clock icon 🕓  in script editor, click +Add trigger and choose Select event type > On change > Save.

Note that the trigger will run every time any change is made to the spreadsheet. You will have to carefully examine the event object and the values returned by the various SpreadsheetApp.getActive* methods to determine whether you want your script to do its thing when it gets run through the trigger. When the event is related to filters, the changeType property of the event object will be OTHER.