Option #1. Slow [~16 sec], no API needed
I've tried this code:
function deleteFilterCriterias(sheet)
{
var filter = sheet.getFilter();
if (!(filter)) { return -1; }
var rangeF = filter.getRange();
var cols = rangeF.getWidth();
var col = rangeF.getColumn();
for (var i = col; i <= cols; i++)
{
// remove filter criteria for each column
filter.removeColumnFilterCriteria(i)
}
return 0;
}
But it seems clunky to me.
Option #2. Fast [~0.3 sec], need Sheets API
After the answer by @Tanaike, I've tried the Sheets API.
My code to reset filter on one sheet is:
function deleteFilterCriterias2(sheet)
{
var ssId = sheet.getParent().getId();
var range = sheet.getFilter().getRange();
var rowStart = range.getRow() - 1;
var colStart = range.getColumn() - 1;
// settings to reset filter
var filterSettings = {
"range": {
"sheetId": sheet.getSheetId(),
"startRowIndex": rowStart,
"endRowIndex": range.getHeight() + rowStart,
"startColumnIndex": colStart + colStart,
"endColumnIndex": range.getWidth()
}
};
var requests = [{
"setBasicFilter": {
"filter": filterSettings
}
}];
// api request
Sheets.Spreadsheets.batchUpdate({'requests': requests}, ssId);
}
This one is much faster.
References
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…