Is there a way to enable data filters by script in a Google Sheet for a certain range? Currently this can be doen manually, but I do not want to select a range, then click 'Data', and then turn on filter.
I know that the filter will remain in an existing sheet. However, I try to apply a filter for a new spreadsheet that is generated via scripting.
My idea is:
function foo() {
var spreadsheet = SpreadsheetApp.getActive();
var infoSheet = spreadsheet.insertSheet('sheetName', spreadsheet.getNumSheets());
infoSheet.getRange(1, 1, 5, 5). -> enable filter?
...
}
How can I achieve my goal programmatically?
A short answer would be "No". It seems that GAS supports neither getting, nor setting Filter criteria for columns, at least according to this open issue.
An alternative solution would be to do the filtering and sorting on GAS side, and push the distinct results into different columns or sheets.
Now you can apply filters using google sheets advanced service. First you need to turn on the Google Sheets API. For that follow the steps as mentioned in the below link:
https://developers.google.com/sheets/api/quickstart/apps-script
After that you can use the following function to apply filters.
Filter
s are now also available in the native Spreadsheet Service, without needing to activate the Sheets REST API via "Advanced Services".The above Sheets REST API method, adapted for native Apps Script:
Further reading: