I am working on getting filters set through Google Apps Script. From my research I have come to the conclusion that although .setVisibleValues()
is listed as available, it is not yet supported. The only way to programmatically filter a column would be to use .setHiddenValues()
. This presents a challenge because there can be hundreds of values that will need to be hidden.
In the example code below I have chosen to exclude values One, Two, Three, Five, Six, and Seven in column 12 (L). If there are only seven values in that column, this should return a filtered data set with only "Four" in column L.
function testFilter() {
var spreadsheet = SpreadsheetApp.getActive();
var criteria = SpreadsheetApp.newFilterCriteria()
.setHiddenValues(['One', 'Two', 'Three', 'Five', 'Six', 'Seven'])
.build();
spreadsheet.getActiveSheet().getFilter().setColumnFilterCriteria(12, criteria);
};
If using .setHiddenValues()
is the only way, my thought was to build a list of items to exclude that do not include a certain value or values. In other words, if the values in column L do not equal 'Four' include in the list of .setHiddenValues()
. I imagine this will require a loop but I wanted to see what the thoughts were. I am fairly new to GAS so I am not sure how to build an efficient loop that will accomplish this. Is there a better way to set filters?
Using I'-'I's method worked but I found that filtering on numeric columns did not work. The solution was answered on this question.
Here is the solution provided by Tanaike. Replace the getHiddenValueArray function with this:
I have also created the following to search for a string within a cell. The above code seems to look for exact matches. I'm sure there is a way to combine the two and there may be a way to improve the following, but it works. Feel free to comment if there is a better way.
In some cases I want an exact match (first solution). Others I don't (second solution).
Yes. You can use
splice()
method. You can change this from:to:
Another method is to use
filter()
. This will also remove duplicates: