I am trying to run a Google Script to delete unused filter views. I found the script on Stackoverflow
function delUnusedFilterViews() {
var ssId = SpreadsheetApp.getActive().getId();
var sheetName = SpreadsheetApp.getActiveSheet().getName();
SpreadsheetApp.getActiveSpreadsheet().toast('Removing unnamed Filters from sheet: ' + sheetName);
var allFilters = Sheets.Spreadsheets.get(ssId).sheets[0].filterViews;
var filterArr =[];
for (var i in allFilters) {
var currFilter = allFilters[i];
var filterName = currFilter.title;
var currFilterId = currFilter.filterViewId;
if (/Filter [0-9]/.test(filterName)) filterArr.push({ deleteFilterView: { filterId: currFilterId } })
}
Sheets.Spreadsheets.batchUpdate({
requests: filterArr
},
ssId
)
Browser.msgBox("All Done. You need to reload the sheet to see the filters have been deleted");
}
I've never run a Google Script before, so I went through the process of adding in the "Sheets" services. However, I'm getting this error when I run it and I don't know what's going wrong:
GoogleJsonResponseException: API call to sheets.spreadsheets.batchUpdate failed with error: Must specify at least one request.
I'm writing this answer as a community wiki, since the issue was resolved in a different post, but I will explain what the script does.
I tested the script and it works. There are a couple of things that you need to keep in mind when running the script.
var allFilters = Sheets.Spreadsheets.get(ssId).sheets[0].filterViews;you need to specify the sheet where you want to get the unnamed filters, count starts from0, so let's say for example you have unnamed filters inSheet2, you have to changesheets[0]tosheets[1].