Class FilterCriteriaBuilder Stay organized with collections Save and categorize content based on your preferences.
Page Summary
FilterCriteriaBuilder is used to create filter criteria for spreadsheets.
To create filter criteria, you must use SpreadsheetApp.newFilterCriteria(), add settings using the builder's methods, and then use build() to assemble the criteria.
Common uses include hiding values and showing only non-empty cells.
The builder offers methods for setting criteria based on values, colors, dates, numbers, text, formulas, and boolean conditions.
Builder for filter criteria. To add criteria to a filter, you must do the following:
- Create the criteria builder using
Spreadsheet.App.newFilterCriteria() - Add settings to the builder using the methods from this class.
- Use
build()to assemble the criteria with your specified settings.
Common uses
Hide values on a sheet
The following sample gets a sheet's existing filter and adds criteria that hides the cells incolumn C that contain "hello" or "world." The criteria in this sample can only be used withfilters onGrid sheets, the default type of sheet.constss=SpreadsheetApp.getActiveSheet();constfilter=ss.getFilter();constcriteria=SpreadsheetApp.newFilterCriteria().setHiddenValues(['hello','world']).build();filter.setColumnFilterCriteria(3,criteria);
Only show non-empty cells
The following sample adds a filter to aDataSource sheet, a sheet that'sconnected to a database, with criteria that only shows cells in the "Category" column that aren'tempty.// Gets the sheet named "Connected sheet," which is connected to a database.constsheet=SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Connected sheet').asDataSourceSheet();// Creates criteria that only shows non-empty cells.constcriteria=SpreadsheetApp.newFilterCriteria().whenCellNotEmpty().build();// Applies the criteria to the column named "Category."sheet.addFilter('Category',criteria);
Methods
| Method | Return type | Brief description |
|---|---|---|
build() | Filter | Assembles the filter criteria using the settings you add to the criteria builder. |
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. |
set | Filter | Sets the values to hide. |
set | Filter | Sets the background color used as filter criteria. |
set | Filter | Sets the foreground color used as filter criteria. |
set | Filter | Sets the values to show on a pivot table. |
when | Filter | Sets the filter criteria to show empty cells. |
when | Filter | Sets the filter criteria to show cells that aren't empty. |
when | Filter | Sets filter criteria that shows cells with dates that are after the specified date. |
when | Filter | Sets filter criteria that shows cells with dates that are after the specified relative date. |
when | Filter | Sets filter criteria that shows cells with dates that are before the specified date. |
when | Filter | Sets filter criteria that shows cells with dates that are before the specified relative date. |
when | Filter | Sets filter criteria that shows cells with dates that are equal to the specified date. |
when | Filter | Sets filter criteria that shows cells with dates that are equal to the specified relative date. |
when | Filter | Sets the filter criteria to show cells with dates that are equal to any of the specified dates. |
when | Filter | Sets the filter criteria to show cells that aren't equal to the specified date. |
when | Filter | Sets the filter criteria to show cells with dates that aren't equal to any of the specifieddates. |
when | Filter | Sets the filter criteria to show cells with a specified formula (such as=B:B<C:C) thatevaluates totrue. |
when | Filter | Sets the filter criteria to show cells with a number that falls between, or is either of, 2specified numbers. |
when | Filter | Sets the filter criteria to show cells with a number that's equal to the specified number. |
when | Filter | Sets the filter criteria to show cells with a number that's equal to any of the specifiednumbers. |
when | Filter | Sets the filter criteria to show cells with a number greater than the specified number |
when | Filter | Sets the filter criteria to show cells with a number greater than or equal to the specifiednumber. |
when | Filter | Sets the filter criteria to show cells with a number that's less than the specified number. |
when | Filter | Sets the filter criteria to show cells with a number less than or equal to the specifiednumber. |
when | Filter | Sets the filter criteria to show cells with a number doesn't fall between, and is neither of, 2specified numbers. |
when | Filter | Sets the filter criteria to show cells with a number that isn't equal to the specified number. |
when | Filter | Sets the filter criteria to show cells with a number that isn't equal to any of the specifiednumbers. |
when | Filter | Sets the filter criteria to show cells with text that contains the specified text. |
when | Filter | Sets the filter criteria to show cells with text that doesn't contain the specified text. |
when | Filter | Sets the filter criteria to show cells with text that ends with the specified text. |
when | Filter | Sets the filter criteria to show cells with text that's equal to the specified text. |
when | Filter | Sets the filter criteria to show cells with text that's equal to any of the specified textvalues. |
when | Filter | Sets the filter criteria to show cells with text that isn't equal to the specified text. |
when | Filter | Sets the filter criteria to show cells with text that isn't equal to any of the specifiedvalues. |
when | Filter | Sets the filter criteria to show cells with text that starts with the specified text. |
with | Filter | Sets the filter criteria to a boolean condition defined byBoolean values, suchasCELL_EMPTY orNUMBER_GREATER_THAN. |
Detailed documentation
build()
Assembles the filter criteria using the settings you add to the criteria builder.
// Gets the existing filter on the sheet.constss=SpreadsheetApp.getActiveSheet();constfilter=ss.getFilter();constcriteria=SpreadsheetApp.newFilterCriteria()// Creates a criteria builder..whenCellNotEmpty()// Adds settings to the builder..build();// Assembles the criteria.filter.setColumnFilterCriteria(2,criteria);
Return
Filter — A representation of the filter criteria.
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
with.Criteria(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
with.Criteria(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.
setHiddenValues(values)
Sets the values to hide. Clears any existing visible or hidden values.
You can only use this criteria for filters onGrid sheets, the defaulttype of sheet.
// Gets the existing filter on the range.constss=SpreadsheetApp.getActiveSheet();constrange=ss.getRange('A1:C20');constfilter=range.getFilter();// Sets the values to hide and applies the criteria to column C.constcriteria=SpreadsheetApp.newFilterCriteria().setHiddenValues(['Hello','World']).build();filter.setColumnFilterCriteria(3,criteria);
Parameters
| Name | Type | Description |
|---|---|---|
values | String[] | The list of values to hide. |
Return
Filter — This builder, for chaining.
Throws
Error — If any of the values arenull.
setVisibleBackgroundColor(visibleBackgroundColor)
Sets the background color used as filter criteria. Cells with this background color remainvisible. Setting a background color filter criteria removes any current color filter criteriafrom this builder.
You can only use this criteria for filters onGrid sheets, the defaulttype of sheet.
// Gets the existing filter on the sheet.constss=SpreadsheetApp.getActiveSheet();constfilter=ss.getFilter();// Creates criteria that filters by background color and sets it to column B.constcolor=SpreadsheetApp.newColor().setRgbColor('#185ABC').build();constcriteria=SpreadsheetApp.newFilterCriteria().setVisibleBackgroundColor(color).build();filter.setColumnFilterCriteria(2,criteria);
Parameters
| Name | Type | Description |
|---|---|---|
visible | Color | The background color to set. The color must be an RGB-style color. This method doesn't support theme colors. |
Return
Filter — This builder, for chaining.
setVisibleForegroundColor(visibleForegroundColor)
Sets the foreground color used as filter criteria. Cells with this foreground color remainvisible. Setting a foreground color filter criteria removes any current color filter criteriafrom this builder.
You can only use this criteria for filters onGrid sheets, the defaulttype of sheet.
// Gets the existing filter on the sheet.constss=SpreadsheetApp.getActiveSheet();constfilter=ss.getFilter();// Creates criteria that filters by foreground color and sets it to column B.constcolor=SpreadsheetApp.newColor().setRgbColor('#185ABC').build();constcriteria=SpreadsheetApp.newFilterCriteria().setVisibleForegroundColor(color).build();filter.setColumnFilterCriteria(2,criteria);
Parameters
| Name | Type | Description |
|---|---|---|
visible | Color | The foreground color to set. The color must be an RGB-style color. This method doesn't support theme colors. |
Return
Filter — This builder, for chaining.
setVisibleValues(values)
Sets the values to show on a pivot table. Clears any existing visible or hidden values.
You can only use this criteria for filters on pivot tables that aren't connected to adatabase.
// Gets the active sheet.constss=SpreadsheetApp.getActiveSheet();// Gets the first pivot table on the sheet and adds a filter to it that// sets the visible values to "Northeast" and "Southwest."constpivotTable=ss.getPivotTables()[0];constcriteria=SpreadsheetApp.newFilterCriteria().setVisibleValues(['Northeast','Southwest']).build();pivotTable.addFilter(2,criteria);
Parameters
| Name | Type | Description |
|---|---|---|
values | String[] | The list of values to show. |
Return
Filter — This builder, for chaining.
Throws
Error — If any of the values arenull.
whenCellEmpty()
Sets the filter criteria to show empty cells.
You can use this criteria with any type of filter.
// Gets the existing filter on the range.constss=SpreadsheetApp.getActiveSheet();constrange=ss.getRange('A1:C20');constfilter=range.getFilter();// Sets criteria to column B that only shows empty cells.constcriteria=SpreadsheetApp.newFilterCriteria().whenCellEmpty().build();filter.setColumnFilterCriteria(2,criteria);
Return
Filter — This builder, for chaining.
whenCellNotEmpty()
Sets the filter criteria to show cells that aren't empty.
You can use this criteria with any type of filter.
// Gets the existing filter on the range.constss=SpreadsheetApp.getActiveSheet();constrange=ss.getRange('A1:C20');constfilter=range.getFilter();// Sets criteria to column B that only shows cells that aren't empty.constcriteria=SpreadsheetApp.newFilterCriteria().whenCellNotEmpty().build();filter.setColumnFilterCriteria(2,criteria);
Return
Filter — This builder, for chaining.
whenDateAfter(date)
Sets filter criteria that shows cells with dates that are after the specified date.
You can use this criteria with any type of filter. If you use this criteria with data that'sconnected to a database, the data type of the column you're filtering by must be a date. If thedata isn't connected to a database, the data type of the column you're filtering by doesn'tneed to be a date, but if it's not, you might get unexpected results.
// Gets the existing filter on the range.constss=SpreadsheetApp.getActiveSheet();constrange=ss.getRange('A1:C20');constfilter=range.getFilter();// Creates criteria that only shows cells with dates after June 1, 2022// and sets it to column A.constdate=newDate('June 1, 2022');constcriteria=SpreadsheetApp.newFilterCriteria().whenDateAfter(date).build();filter.setColumnFilterCriteria(1,criteria);
Parameters
| Name | Type | Description |
|---|---|---|
date | Date | The latest date to hide. |
Return
Filter — This builder, for chaining.
whenDateAfter(date)
Sets filter criteria that shows cells with dates that are after the specified relative date. Toview the relative date options, refer toEnumRelative.
You can use this criteria with any type of filter. If you use this criteria with data that'sconnected to a database, the data type of the column you're filtering by must be a date. If thedata isn't connected to a database, the data type of the column you're filtering by doesn'tneed to be a date, but if it's not, you might get unexpected results.
// Gets the existing filter on the range.constss=SpreadsheetApp.getActiveSheet();constrange=ss.getRange('A1:C20');constfilter=range.getFilter();// Creates criteria that only shows cells with dates after today's date// and sets it to column A.constcriteria=SpreadsheetApp.newFilterCriteria().whenDateAfter(SpreadsheetApp.RelativeDate.TODAY).build();filter.setColumnFilterCriteria(1,criteria);
Parameters
| Name | Type | Description |
|---|---|---|
date | Relative | The latest relative date. |
Return
Filter — This builder, for chaining.
whenDateBefore(date)
Sets filter criteria that shows cells with dates that are before the specified date.
You can use this criteria with any type of filter. If you use this criteria with data that'sconnected to a database, the data type of the column you're filtering by must be a date. If thedata isn't connected to a database, the data type of the column you're filtering by doesn'tneed to be a date, but if it's not, you might get unexpected results.
// Gets the existing filter on the range.constss=SpreadsheetApp.getActiveSheet();constrange=ss.getRange('A1:C20');constfilter=range.getFilter();// Creates criteria that only shows cells with dates before June 1, 2022// and sets it to column A.constdate=newDate('June 1, 2022');constcriteria=SpreadsheetApp.newFilterCriteria().whenDateBefore(date).build();filter.setColumnFilterCriteria(1,criteria);
Parameters
| Name | Type | Description |
|---|---|---|
date | Date | The earliest date to hide. |
Return
Filter — This builder, for chaining.
whenDateBefore(date)
Sets filter criteria that shows cells with dates that are before the specified relative date.To view the relative date options, refer toEnumRelative.
You can use this criteria with any type of filter. If you use this criteria with data that'sconnected to a database, the data type of the column you're filtering by must be a date. If thedata isn't connected to a database, the data type of the column you're filtering by doesn'tneed to be a date, but if it's not, you might get unexpected results.
// Gets the existing filter on the range.constss=SpreadsheetApp.getActiveSheet();constrange=ss.getRange('A1:C20');constfilter=range.getFilter();// Creates criteria that only shows cells with dates before today's date// and sets it to column A.constcriteria=SpreadsheetApp.newFilterCriteria().whenDateBefore(SpreadsheetApp.RelativeDate.TODAY).build();filter.setColumnFilterCriteria(1,criteria);
Parameters
| Name | Type | Description |
|---|---|---|
date | Relative | The earliest relative date to hide. |
Return
Filter — This builder, for chaining.
whenDateEqualTo(date)
Sets filter criteria that shows cells with dates that are equal to the specified date.
You can use this criteria with any type of filter. If you use this criteria with data that'sconnected to a database, the data type of the column you're filtering by must be a date. If thedata isn't connected to a database, the data type of the column you're filtering by doesn'tneed to be a date, but if it's not, you might get unexpected results.
// Gets the existing filter on the range.constss=SpreadsheetApp.getActiveSheet();constrange=ss.getRange('A1:C20');constfilter=range.getFilter();// Creates criteria that only shows cells with dates equal to June 1, 2022// and sets it to column A.constdate=newDate('June 1, 2022');constcriteria=SpreadsheetApp.newFilterCriteria().whenDateEqualTo(date).build();filter.setColumnFilterCriteria(1,criteria);
Parameters
| Name | Type | Description |
|---|---|---|
date | Date | The date that cell values must match. |
Return
Filter — This builder, for chaining.
whenDateEqualTo(date)
Sets filter criteria that shows cells with dates that are equal to the specified relative date.To view the relative date options, refer toEnumRelative.
You can use this criteria with any type of filter. If you use this criteria with data that'sconnected to a database, the data type of the column you're filtering by must be a date. If thedata isn't connected to a database, the data type of the column you're filtering by doesn'tneed to be a date, but if it's not, you might get unexpected results.
// Gets the existing filter on the range.constss=SpreadsheetApp.getActiveSheet();constrange=ss.getRange('A1:C20');constfilter=range.getFilter();// Creates criteria that only shows cells with dates that fall within the past// month and sets it to column A.constcriteria=SpreadsheetApp.newFilterCriteria().whenDateEqualTo(SpreadsheetApp.RelativeDate.PAST_MONTH).build();filter.setColumnFilterCriteria(1,criteria);
Parameters
| Name | Type | Description |
|---|---|---|
date | Relative | The relative date that cell values must match. |
Return
Filter — This builder, for chaining.
whenDateEqualToAny(dates)
Sets the filter criteria to show cells with dates that are equal to any of the specified dates.
You can only use this criteria with data connected to a database. For example, use thiscriteria with filters on aData sheet, a sheet that's connected to adatabase, or aData, a pivot table created from aData sheet.
// Gets the sheet that's connected to a database.constss=SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Data Sheet');constdataSheet=ss.asDataSourceSheet();// Adds criteria to the "date" column that shows cells with any of the below// dates.constdate1=newDate('June 1, 2022');constdate2=newDate('June 2, 2022');constdate3=newDate('June 3, 2022');constcriteria=SpreadsheetApp.newFilterCriteria().whenDateEqualToAny([date1,date2,date3]).build();dataSheet.addFilter('date',criteria);
Parameters
| Name | Type | Description |
|---|---|---|
dates | Date[] | The dates to show. |
Return
Filter — This builder, for chaining.
whenDateNotEqualTo(date)
Sets the filter criteria to show cells that aren't equal to the specified date.
You can only use this criteria with data connected to a database. For example, use thiscriteria with filters on aData sheet, a sheet that's connected to adatabase, or aData, a pivot table created from aData sheet.
The data type of the column you're filtering by must be a date.
// Gets a pivot table that's connected to a database.constss=SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Pivot Table Sheet');constdataPivotTable=ss.getDataSourcePivotTables()[0];// Creates criteria that only shows cells that don't equal June 16, 2022// and sets it to the "date" column.constdate=newDate('June 16, 2022');constcriteria=SpreadsheetApp.newFilterCriteria().whenDateNotEqualTo(date).build();dataPivotTable.addFilter('date',criteria);
Parameters
| Name | Type | Description |
|---|---|---|
date | Date | The date to hide. |
Return
Filter — This builder, for chaining.
whenDateNotEqualToAny(dates)
Sets the filter criteria to show cells with dates that aren't equal to any of the specifieddates.
You can only use this criteria with data connected to a database. For example, use thiscriteria with filters on aData sheet, a sheet that's connected to adatabase, or aData, a pivot table created from aData sheet.
// Gets the sheet that's connected to a database.constss=SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Data Sheet');constdataSheet=ss.asDataSourceSheet();// Adds criteria to the "date" column that hides cells with any of the below// dates.constdate1=newDate('June 1, 2022');constdate2=newDate('June 2, 2022');constdate3=newDate('June 3, 2022');constcriteria=SpreadsheetApp.newFilterCriteria().whenDateNotEqualToAny([date1,date2,date3]).build();dataSheet.addFilter('date',criteria);
Parameters
| Name | Type | Description |
|---|---|---|
dates | Date[] | The dates to hide. |
Return
Filter — This builder, for chaining.
whenFormulaSatisfied(formula)
Sets the filter criteria to show cells with a specified formula (such as=B:B<C:C) thatevaluates totrue.
You can only use this criteria to filter data that isn't connected to a database.
// Gets the existing filter on the sheet.constss=SpreadsheetApp.getActiveSheet();constfilter=ss.getFilter();// Creates criteria that shows the rows where the value in column B is less than// the value in column C and sets it to column A.constformula='=B:B<C:C';constcriteria=SpreadsheetApp.newFilterCriteria().whenFormulaSatisfied(formula).build();filter.setColumnFilterCriteria(1,criteria);
Parameters
| Name | Type | Description |
|---|---|---|
formula | String | A custom formula that evaluates totrue if the input is valid. |
Return
Filter — This builder, for chaining.
whenNumberBetween(start, end)
Sets the filter criteria to show cells with a number that falls between, or is either of, 2specified numbers.
You can use this criteria with any type of filter. If you use this criteria with data that'sconnected to a database, the data type of the column you're filtering by must be a number. Ifthe data isn't connected to a database, the data type of the column you're filtering by doesn'tneed to be a number, but if it's not, you might get unexpected results.
// Gets the existing filter on the sheet.constss=SpreadsheetApp.getActiveSheet();constfilter=ss.getFilter();// Creates criteria that only shows cells with numbers that fall between 1-25,// inclusively, and sets it to column A.constcriteria=SpreadsheetApp.newFilterCriteria().whenNumberBetween(1,25).build();filter.setColumnFilterCriteria(1,criteria);
Parameters
| Name | Type | Description |
|---|---|---|
start | Number | The lowest number to show. |
end | Number | The highest number to show. |
Return
Filter — This builder, for chaining.
whenNumberEqualTo(number)
Sets the filter criteria to show cells with a number that's equal to the specified number.
You can use this criteria with any type of filter. If you use this criteria with data that'sconnected to a database, the data type of the column you're filtering by must be a number. Ifthe data isn't connected to a database, the data type of the column you're filtering by doesn'tneed to be a number, but if it's not, you might get unexpected results.
// Gets the existing filter on the sheet.constss=SpreadsheetApp.getActiveSheet();constfilter=ss.getFilter();// Creates criteria that only shows cells that are equal to 25 and sets it to// column B.constcriteria=SpreadsheetApp.newFilterCriteria().whenNumberEqualTo(25).build();filter.setColumnFilterCriteria(2,criteria);
Parameters
| Name | Type | Description |
|---|---|---|
number | Number | The number to show. |
Return
Filter — This builder, for chaining.
whenNumberEqualToAny(numbers)
Sets the filter criteria to show cells with a number that's equal to any of the specifiednumbers.
You can only use this criteria with data connected to a database. For example, use thiscriteria with filters on aData sheet, a sheet that's connected to adatabase, or aData, a pivot table created from aData sheet.
// Gets the sheet that's connected to a database.constss=SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Data Sheet');constdataSheet=ss.asDataSourceSheet();// Adds criteria to the "amount" column that only shows cells with the number// 10, 20, or 30.constcriteria=SpreadsheetApp.newFilterCriteria().whenNumberEqualToAny([10,20,30]).build();dataSheet.addFilter('amount',criteria);
Parameters
| Name | Type | Description |
|---|---|---|
numbers | Number[] | The numbers to show. |
Return
Filter — This builder, for chaining.
whenNumberGreaterThan(number)
Sets the filter criteria to show cells with a number greater than the specified number
You can use this criteria with any type of filter. If you use this criteria with data that'sconnected to a database, the data type of the column you're filtering by must be a number. Ifthe data isn't connected to a database, the data type of the column you're filtering by doesn'tneed to be a number, but if it's not, you might get unexpected results.
// Gets the existing filter on the sheet.constss=SpreadsheetApp.getActiveSheet();constfilter=ss.getFilter();// Creates criteria that shows cells greater than 10 and sets it to column B.constcriteria=SpreadsheetApp.newFilterCriteria().whenNumberGreaterThan(10).build();filter.setColumnFilterCriteria(2,criteria);
Parameters
| Name | Type | Description |
|---|---|---|
number | Number | The highest number to hide. |
Return
Filter — This builder, for chaining.
whenNumberGreaterThanOrEqualTo(number)
Sets the filter criteria to show cells with a number greater than or equal to the specifiednumber.
You can use this criteria with any type of filter. If you use this criteria with data that'sconnected to a database, the data type of the column you're filtering by must be a number. Ifthe data isn't connected to a database, the data type of the column you're filtering by doesn'tneed to be a number, but if it's not, you might get unexpected results.
// Gets the existing filter on the sheet.constss=SpreadsheetApp.getActiveSheet();constfilter=ss.getFilter();// Creates criteria that shows cells greater than or equal to 10 and sets it to// column B.constcriteria=SpreadsheetApp.newFilterCriteria().whenNumberGreaterThanOrEqualTo(10).build();filter.setColumnFilterCriteria(2,criteria);
Parameters
| Name | Type | Description |
|---|---|---|
number | Number | The lowest number to show. |
Return
Filter — This builder, for chaining.
whenNumberLessThan(number)
Sets the filter criteria to show cells with a number that's less than the specified number.
You can use this criteria with any type of filter. If you use this criteria with data that'sconnected to a database, the data type of the column you're filtering by must be a number. Ifthe data isn't connected to a database, the data type of the column you're filtering by doesn'tneed to be a number, but if it's not, you might get unexpected results.
// Gets the existing filter on the sheet.constss=SpreadsheetApp.getActiveSheet();constfilter=ss.getFilter();// Creates criteria that shows cells less than 10 and sets it to column B.constcriteria=SpreadsheetApp.newFilterCriteria().whenNumberLessThan(10).build();filter.setColumnFilterCriteria(2,criteria);
Parameters
| Name | Type | Description |
|---|---|---|
number | Number | The lowest number to hide. |
Return
Filter — This builder, for chaining.
whenNumberLessThanOrEqualTo(number)
Sets the filter criteria to show cells with a number less than or equal to the specifiednumber.
You can use this criteria with any type of filter. If you use this criteria with data that'sconnected to a database, the data type of the column you're filtering by must be a number. Ifthe data isn't connected to a database, the data type of the column you're filtering by doesn'tneed to be a number, but if it's not, you might get unexpected results.
// Gets the existing filter on the sheet.constss=SpreadsheetApp.getActiveSheet();constfilter=ss.getFilter();// Creates criteria that shows cells less than or equal to 10 and sets it to// column B.constcriteria=SpreadsheetApp.newFilterCriteria().whenNumberLessThanOrEqualTo(10).build();filter.setColumnFilterCriteria(2,criteria);
Parameters
| Name | Type | Description |
|---|---|---|
number | Number | The highest number to show. |
Return
Filter — This builder, for chaining.
whenNumberNotBetween(start, end)
Sets the filter criteria to show cells with a number doesn't fall between, and is neither of, 2specified numbers.
You can use this criteria with any type of filter. If you use this criteria with data that'sconnected to a database, the data type of the column you're filtering by must be a number. Ifthe data isn't connected to a database, the data type of the column you're filtering by doesn'tneed to be a number, but if it's not, you might get unexpected results.
// Gets the existing filter on the sheet.constss=SpreadsheetApp.getActiveSheet();constfilter=ss.getFilter();// Creates criteria that hides cells with numbers that fall between 1-25,// inclusively, and sets it to column B.constcriteria=SpreadsheetApp.newFilterCriteria().whenNumberNotBetween(1,25).build();filter.setColumnFilterCriteria(2,criteria);
Parameters
| Name | Type | Description |
|---|---|---|
start | Number | The lowest number hide. |
end | Number | The highest number to hide. |
Return
Filter — This builder, for chaining.
whenNumberNotEqualTo(number)
Sets the filter criteria to show cells with a number that isn't equal to the specified number.
You can use this criteria with any type of filter. If you use this criteria with data that'sconnected to a database, the data type of the column you're filtering by must be a number. Ifthe data isn't connected to a database, the data type of the column you're filtering by doesn'tneed to be a number, but if it's not, you might get unexpected results.
// Gets the existing filter on the sheet.constss=SpreadsheetApp.getActiveSheet();constfilter=ss.getFilter();// Creates criteria that hides cells that are equal to 25 and sets it to column// B.constcriteria=SpreadsheetApp.newFilterCriteria().whenNumberNotEqualTo(25).build();filter.setColumnFilterCriteria(2,criteria);
Parameters
| Name | Type | Description |
|---|---|---|
number | Number | The number to hide. |
Return
Filter — This builder, for chaining.
whenNumberNotEqualToAny(numbers)
Sets the filter criteria to show cells with a number that isn't equal to any of the specifiednumbers.
You can only use this criteria with data connected to a database. For example, use thiscriteria with filters on aData sheet, a sheet that's connected to adatabase, or aData, a pivot table created from aData sheet.
// Gets the sheet that's connected to a database.constss=SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Data Sheet');constdataSheet=ss.asDataSourceSheet();// Adds criteria to the "amount" column that hides cells with the number 10, 20,// or 30.constcriteria=SpreadsheetApp.newFilterCriteria().whenNumberNotEqualToAny([10,20,30]).build();dataSheet.addFilter('amount',criteria);
Parameters
| Name | Type | Description |
|---|---|---|
numbers | Number[] | The numbers to hide. |
Return
Filter — This builder, for chaining.
whenTextContains(text)
Sets the filter criteria to show cells with text that contains the specified text. The textisn't case-sensitive.
You can use this criteria with any type of filter.
// Gets the existing filter on the sheet.constss=SpreadsheetApp.getActiveSheet();constfilter=ss.getFilter();// Creates criteria that shows cells that contain "Northwest" and sets it to// column B.constcriteria=SpreadsheetApp.newFilterCriteria().whenTextContains('Northwest').build();filter.setColumnFilterCriteria(2,criteria);
Parameters
| Name | Type | Description |
|---|---|---|
text | String | The text that the cell must contain. |
Return
Filter — This builder, for chaining.
whenTextDoesNotContain(text)
Sets the filter criteria to show cells with text that doesn't contain the specified text. Thetext isn't case-sensitive.
You can use this criteria with any type of filter.
// Gets the existing filter on the sheet.constss=SpreadsheetApp.getActiveSheet();constfilter=ss.getFilter();// Creates criteria that hides cells that contain "Northwest" and sets it to// column B.constcriteria=SpreadsheetApp.newFilterCriteria().whenTextDoesNotContain('Northwest').build();filter.setColumnFilterCriteria(2,criteria);
Parameters
| Name | Type | Description |
|---|---|---|
text | String | The text that the cell must not contain. |
Return
Filter — This builder, for chaining.
whenTextEndsWith(text)
Sets the filter criteria to show cells with text that ends with the specified text. The textisn't case-sensitive.
You can use this criteria with any type of filter.
// Gets the existing filter on the sheet.constss=SpreadsheetApp.getActiveSheet();constfilter=ss.getFilter();// Creates criteria that shows cells with text that ends with "est" and sets it// to column B.constcriteria=SpreadsheetApp.newFilterCriteria().whenTextEndsWith('est').build();filter.setColumnFilterCriteria(2,criteria);
Parameters
| Name | Type | Description |
|---|---|---|
text | String | Text that the end of the cell's text must contain. |
Return
Filter — This builder, for chaining.
whenTextEqualTo(text)
Sets the filter criteria to show cells with text that's equal to the specified text. The textisn't case-sensitive.
You can use this criteria with any type of filter.
// Gets the existing filter on the sheet.constss=SpreadsheetApp.getActiveSheet();constfilter=ss.getFilter();// Creates criteria that shows cells with text that equals "hello" and sets it// to column B.constcriteria=SpreadsheetApp.newFilterCriteria().whenTextEqualTo('hello').build();filter.setColumnFilterCriteria(2,criteria);
Parameters
| Name | Type | Description |
|---|---|---|
text | String | The text that the cell's text must equal. |
Return
Filter — This builder, for chaining.
whenTextEqualToAny(texts)
Sets the filter criteria to show cells with text that's equal to any of the specified textvalues. The text isn't case-sensitive.
You can only use this criteria with data connected to a database. For example, use thiscriteria with filters on aData sheet, a sheet that's connected to adatabase, or aData, a pivot table created from aData sheet.
// Gets the sheet that's connected to a database.constss=SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Data Sheet');constdataSheet=ss.asDataSourceSheet();// Adds criteria to the "category" column that shows cells with the text "tech"// or "business."constcriteria=SpreadsheetApp.newFilterCriteria().whenTextEqualToAny(['tech','business']).build();dataSheet.addFilter('category',criteria);
Parameters
| Name | Type | Description |
|---|---|---|
texts | String[] | The text values that a cell must equal. |
Return
Filter — This builder, for chaining.
whenTextNotEqualTo(text)
Sets the filter criteria to show cells with text that isn't equal to the specified text. Thetext isn't case-sensitive.
You can only use this criteria with data connected to a database. For example, use thiscriteria with filters on aData sheet, a sheet that's connected to adatabase, or aData, a pivot table created from aData sheet.
// Gets the sheet that's connected to a database.constss=SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Data Sheet');constdataSheet=ss.asDataSourceSheet();// Adds criteria to the "category" column that hides cells with text equal to// "tech."constcriteria=SpreadsheetApp.newFilterCriteria().whenTextNotEqualTo('tech').build();dataSheet.addFilter('category',criteria);
Parameters
| Name | Type | Description |
|---|---|---|
text | String | The text that the cell's text can't equal. |
Return
Filter — This builder, for chaining.
whenTextNotEqualToAny(texts)
Sets the filter criteria to show cells with text that isn't equal to any of the specifiedvalues. The text isn't case-sensitive.
You can only use this criteria with data connected to a database. For example, use thiscriteria with filters on aData sheet, a sheet that's connected to adatabase, or aData, a pivot table created from aData sheet.
// Gets the sheet that's connected to a database.constss=SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Data Sheet');constdataSheet=ss.asDataSourceSheet();// Adds criteria to the "category" column that hides cells with the text "tech"// or "business."constcriteria=SpreadsheetApp.newFilterCriteria().whenTextNotEqualToAny(['tech','business']).build();dataSheet.addFilter('category',criteria);
Parameters
| Name | Type | Description |
|---|---|---|
texts | String[] | The text values that a cell can't equal. |
Return
Filter — This builder, for chaining.
whenTextStartsWith(text)
Sets the filter criteria to show cells with text that starts with the specified text. The textisn't case-sensitive.
You can use this criteria with any type of filter.
// Gets the existing filter on the sheet.constss=SpreadsheetApp.getActiveSheet();constfilter=ss.getFilter();// Creates criteria that shows cells with text that starts with "pre" and sets// it to column B.constcriteria=SpreadsheetApp.newFilterCriteria().whenTextStartsWith('pre').build();filter.setColumnFilterCriteria(2,criteria);
Parameters
| Name | Type | Description |
|---|---|---|
text | String | Text that the start of the cell's text must contain. |
Return
Filter — This builder, for chaining.
withCriteria(criteria, args)
Sets the filter criteria to a boolean condition defined byBoolean values, suchasCELL_EMPTY orNUMBER_GREATER_THAN. To copy the boolean condition fromexisting criteria, define the parameters for this method usingget andget on anexisting criteria.
You can use this criteria with any type of filter, but someBoolean aren'tapplicable for all filters.
// Builds a filter criteria that is based on existing boolean conditions from// another criteria. Gets the existing filter on the sheet.constss=SpreadsheetApp.getActiveSheet();constfilter=ss.getFilter();// Gets the existing boolean conditions applied to Column B and adds criteria to// column C that has the same boolean conditions and additional criteria that// hides the value, "Northwest."constfilterCriteria=filter.getColumnFilterCriteria(2);constcriteria=SpreadsheetApp.newFilterCriteria().withCriteria(filterCriteria.getCriteriaType(),filterCriteria.getCriteriaValues(),).setHiddenValues(['Northwest']).build();filter.setColumnFilterCriteria(3,criteria);
Parameters
| Name | Type | Description |
|---|---|---|
criteria | Boolean | The type of boolean criteria. |
args | Object[] | An array of arguments appropriate to the criteria type; the number of arguments and their type match the correspondingwhen...() methods above. |
Return
Filter — This builder, for chaining.
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.