Class FilterCriteria Stay organized with collections Save and categorize content based on your preferences.
Page Summary
FilterCriteria allows you to get information about or copy the criteria on existing filters.
You can create criteria for any filter type using
SpreadsheetApp.newFilterCriteria()andFilterCriteriaBuilder.You can copy filter criteria from one column to another using the
copy()method.The
getHiddenValues()method returns the values that a filter hides in Grid sheets.Methods like
getVisibleBackgroundColor()andgetVisibleForegroundColor()return color-based filter criteria for Grid sheets.
Use this class to get information about or copy the criteria on existing filters.
- To create a new filter, refer to the following:
- For sheets, use
Range.createFilter(). - For pivot tables, use
Pivot.Table.addFilter(sourceDataColumn, filterCriteria) - For sheets that are connected to a database, use
Data.Source Sheet.addFilter(columnName, filterCriteria) - For pivot tables that are connected to a database, use
Data.Source Pivot Table.addFilter(columnName, filterCriteria)
- For sheets, use
- To create the criteria for any type of filter, refer to
SpreadsheetandApp.newFilterCriteria() Filter.Criteria Builder
Common uses
Copy criteria
The following sample gets the filter that applies to the rangeA1:C20, gets the criteriaapplied to column C, and copies the criteria to column B.constss=SpreadsheetApp.getActiveSheet();constrange=ss.getRange('A1:C20');// Copies the filter criteria applied to column C.constfilter=range.getFilter();constcriteria=filter.getColumnFilterCriteria(3).copy().build();// Applies the copied criteria to column B. The copied criteria overwrites any// existing criteria on column B.filter.setColumnFilterCriteria(2,criteria);
Get values hidden by the filter
The following sample gets the filter that applies to the given range and logs the values fromcolumn B that the filter hides.constss=SpreadsheetApp.getActiveSheet();constrange=ss.getRange('A1:C20');constfilter=range.getFilter();// Gets the filter criteria applied to column B, then gets the hidden values.constfilterCriteria=filter.getColumnFilterCriteria(2).getHiddenValues();// Logs the hidden values.console.log(filterCriteria);
Methods
| Method | Return type | Brief description |
|---|---|---|
copy() | Filter | Copies this filter criteria and creates a criteria builder that you can apply to anotherfilter. |
get | Boolean | Returns the criteria's boolean type, for example,CELL_EMPTY. |
get | Object[] | Returns an array of arguments for boolean criteria. |
get | String[] | Returns the values that the filter hides. |
get | Color|null | Returns the background color used as filter criteria. |
get | Color|null | Returns the foreground color used as a filter criteria. |
get | String[] | Returns the values that the pivot table filter shows. |
Detailed documentation
copy()
Copies this filter criteria and creates a criteria builder that you can apply to anotherfilter.
You can use this method with any type of filter. If you're using a sheet filter, you cancopy the criteria to another column.
constss=SpreadsheetApp.getActiveSheet();constfilter=ss.getFilter();// Makes a copy of the filter criteria applied to column C.constcriteria=filter.getColumnFilterCriteria(3).copy().build();// Applies the copied criteria to column B. The copied criteria overwrites any// existing criteria on column B.filter.setColumnFilterCriteria(2,criteria);
Return
Filter — A filter criteria builder based on this filter criteria.
getCriteriaType()
Returns the criteria's boolean type, for example,CELL_EMPTY. To learn about the typesof boolean criteria, see theBoolean enum.
People often use this method to add boolean condition criteria to a filter without replacingexisting criteria.
- To get the arguments for the criteria, use
get.Criteria Values() - To use the criteria type and criteria values to create or modify filter criteria, see
Filter.Criteria Builder.withCriteria(criteria, args)
You can use this method for any type of filter. If the filter criteria isn't a booleancondition, returnsnull.
constss=SpreadsheetApp.getActiveSheet();// Gets the filter on the active sheet.constfilter=ss.getFilter();// Gets the criteria type and returns a string representing the criteria type// object.constcriteriaType=filter.getColumnFilterCriteria(2).getCriteriaType().toString();// Logs the criteria type.console.log(criteriaType);
Return
Boolean — The type of boolean criteria, ornull if the criteria isn't a boolean condition.
getCriteriaValues()
Returns an array of arguments for boolean criteria. Some boolean criteria types don't havearguments and return an empty array, for example,CELL_NOT_EMPTY.
People often use this method to add boolean condition criteria to a filter without replacingexisting criteria.
- To get the boolean criteria type, use
get.Criteria Type() - To use the criteria type and criteria values to create or modify a filter criteria, see
Filter.Criteria Builder.withCriteria(criteria, args) You can use this method for any type of filter.
constss=SpreadsheetApp.getActiveSheet();constfilter=ss.getFilter();// Gets the values of the boolean criteria and logs them. For example, if the// boolean condition is whenNumberGreaterThan(10), then the logged value is 10.constcriteriaValues=filter.getColumnFilterCriteria(2).getCriteriaValues();console.log(criteriaValues);
Return
Object[]— An array of arguments appropriate to the boolean criteria type. The number of arguments and their type match the correspondingwhen...()method of theFilterclass.Criteria Builder
getHiddenValues()
Returns the values that the filter hides.
Use this criteria with filters onGrid sheets, the default type of sheet.Returnsnull if you call this method for other types of filters.
constss=SpreadsheetApp.getActiveSheet();constrange=ss.getRange('A1:C20');constfilter=range.getFilter();// Gets the filter criteria applied to column B, then gets the hidden values.constfilterCriteria=filter.getColumnFilterCriteria(2).getHiddenValues();// Logs the hidden values.console.log(filterCriteria);
Return
String[] — An array of values that the filter hides.
getVisibleBackgroundColor()
Returns the background color used as filter criteria. Cells with this background color remainvisible.
Use this criteria with filters onGrid sheets, the default type of sheet.Returnsnull if you call this method for other types of filters.
constss=SpreadsheetApp.getActiveSheet();constrange=ss.getRange('A1:C20');// Logs the background color that column B is filtered by as a hexadecimal// string.constfilter=range.getFilter();constcolor=filter.getColumnFilterCriteria(2).getVisibleBackgroundColor().asRgbColor().asHexString();console.log(color);
Return
Color|null — The background color used as filter criteria.
getVisibleForegroundColor()
Returns the foreground color used as a filter criteria. Cells with this foreground color remainvisible.
Use this criteria with filters onGrid sheets, the default type of sheet.Returnsnull if you call this method for other types of filters.
constss=SpreadsheetApp.getActiveSheet();constrange=ss.getRange('A1:C20');// Logs the foreground color that column B is filtered by as a hexadecimal// string.constfilter=range.getFilter();constcolor=filter.getColumnFilterCriteria(2).getVisibleForegroundColor().asRgbColor().asHexString();console.log(color);
Return
Color|null — The foreground color used as a filter criteria.
getVisibleValues()
Returns the values that the pivot table filter shows.
This criteria is only for filters on pivot tables that aren't connected to a database.Returns an empty array for other types of filters.
constss=SpreadsheetApp.getActiveSheet();// Gets the first pivot table on the sheet, then gets the visible values of its// first filter.constpivotTable=ss.getPivotTables()[0];constpivotFilterValues=pivotTable.getFilters()[0].getFilterCriteria().getVisibleValues();// Logs the visible values.console.log(pivotFilterValues);
Return
String[] — An array of values that the pivot table filter shows.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-11 UTC.