4
votes

I am completely new in writing scripts for google sheets, so I was hoping some of you could help/guide me a little bit.

So Ideally, I want a script to clear (not remove) ALL filters in my sheet. This is, however, complicated for me to do (If some of you have such a script, I would LOVE to see it :) )

Instead, I made this one (Used recorder):

function Clear_Filter() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('A5').activate();
  spreadsheet.getActiveSheet().getFilter().removeColumnFilterCriteria(1);
  spreadsheet.getRange('B5').activate();
  spreadsheet.getActiveSheet().getFilter().removeColumnFilterCriteria(2);
  spreadsheet.getRange('C5').activate();
  spreadsheet.getActiveSheet().getFilter().removeColumnFilterCriteria(3);
  spreadsheet.getRange('G5').activate();
  spreadsheet.getActiveSheet().getFilter().removeColumnFilterCriteria(7);
  spreadsheet.getRange('J5').activate();
  spreadsheet.getActiveSheet().getFilter().removeColumnFilterCriteria(10);
  spreadsheet.getRange('M5').activate();
  spreadsheet.getActiveSheet().getFilter().removeColumnFilterCriteria(13);
};

So my filter is set in Row 5. First I made the above for all columns (I had 20), but the problem is, that the code is very slow :( So now I am using the columns, that I use the most, when filtering, but the code is still slow. Well the worst thing is, that the code is running one column at a time (which we see in the code), and when the code is finish, I end up in the last column.

Can I do something? I dont want my sheet window keep turning right, when I run the code, and then end up in column M.

I will appreciate any help! Thanks

5
Yes, you can alter your recorded macro. If you don't want to move the active cell, I suggest removing calls that activate ranges. Google Apps Script (Sheets Macros) is JavaScript - feel free to apply programming techniques you would apply in normal JavaScript functions, such as declaring a variable for something you modify often, especially if you would have to repeat words and methods a lot - like spreadsheet.getActiveSheet().getFilter(), for instance. Additionally, there is a lot of reference documentation available - you can even get a direct link to it from the Script Editor Help menu!tehhowch
Thank you so much for the help. I just looked at the page, but I am not sure how to make it work. So I tried the "Clear / remove all filters" code, but I am getting an error saying, that my sheet is not defined. In the code I replaced the "sheetId" with the name of my sheet.Learning_script
A sheet id and a sheet name are not the same thing. Further, a sheet id and a spreadsheet id are also not the same thing. If your question is solved, and you feel the solution is worth writing an answer about (stackoverflow.com/help/how-to-answer), do so and accept it. If it's not worth an answer, consider deletion. If it not resolved (and you haven't fundamentally redefined the specific issue you are having, (stackoverflow.com/help/how-to-ask)), then make sure your question properly and usefully reflects your issue.tehhowch

5 Answers

3
votes

Here is mine. The function does not remove filters. Instead, it clears them as requested.

function clearFilter(sheet) {
  sheet = SpreadsheetApp.getActiveSheet(); //for testing purpose only
  var filter = sheet.getFilter();
  if (filter !== null) {  // tests if there is a filter applied
    var range = filter.getRange(); // prevents exception in case the filter is not applied to all columns
    var firstColumn = range.getColumn();
    var lastColumn = range.getLastColumn();
    for (var i = firstColumn; i < lastColumn; i++) {
      filter.removeColumnFilterCriteria(i);
    }
  Logger.log('All filters cleared')
  }
  else {Logger.log('There is no filter')}
}
0
votes

Reset filters criterea + sort by first column (as default state). And add this action to main menu.

/** @OnlyCurrentDoc */

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var menuEntries = [
    {name: "Reset filters", functionName: "ResetFilters"},
  ];
  ss.addMenu("Custom actions", menuEntries); // add to main menu
}

function ResetFilters() {
  var spreadsheet = SpreadsheetApp.getActive();
  var lastColumn = spreadsheet.getActiveSheet().getLastColumn();
  var filter = spreadsheet.getActiveSheet().getFilter();
  var criteria = SpreadsheetApp.newFilterCriteria().build(); 
  for (var i = 1; i <= lastColumn; i++) {
    filter.setColumnFilterCriteria(i, criteria);
  }
  filter.sort(1, true); // remove this line for disable setting of sorting order
};
0
votes

To clear all

`function turnOffFilter(sheet) {
        for (var index = 1; index < sheet.getLastColumn(); index++) {
          if (sheet.getFilter().getColumnFilterCriteria(index)) {
            sheet.getFilter().removeColumnFilterCriteria(index);
          }
        }
    }`
0
votes

It seems that the answers (e.g. proposed by Birmin) work fine but the script is painfully slow. I find it much faster to reapply the filter:

function clearFilter(sheet) {
  sheet = SpreadsheetApp.getActiveSheet(); //for testing purpose only
  var filter = sheet.getFilter();
  if (filter !== null) {  // tests if there is a filter applied
    var range = filter.getRange();
    filter.remove();
    range.createFilter();
  Logger.log('All filters cleared')
  }
  else {Logger.log('There is no filter')}
}
-1
votes

I, have you tried :

function Clear_Filter() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getFilter().remove();
}