使用此类可获取有关现有过滤条件的信息或复制现有过滤条件。
- 如需创建新过滤条件,请参阅以下内容:
- 对于工作表,请使用
Range.createFilter()。 - 对于数据透视表,请使用
Pivot。Table.addFilter(sourceDataColumn, filterCriteria) - 对于已关联到数据库的工作表,请使用
Data。Source Sheet.addFilter(columnName, filterCriteria) - 对于与数据库相关联的数据透视表,请使用
Data。Source Pivot Table.addFilter(columnName, filterCriteria)
- 对于工作表,请使用
- 如需创建任何类型的过滤条件,请参阅
Spreadsheet和App.newFilterCriteria() Filter。Criteria Builder
常见用途
复制条件
以下示例获取应用于范围A1:C20 的过滤条件、获取应用于列 C 的条件,并将该条件复制到列 B。
const ss = SpreadsheetApp.getActiveSheet(); const range = ss.getRange('A1:C20'); // Copies the filter criteria applied to column C. const filter = range.getFilter(); const criteria = 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);
获取被过滤条件隐藏的值
以下示例获取适用于指定范围的过滤条件,并记录过滤条件隐藏的 B 列中的值。const ss = SpreadsheetApp.getActiveSheet(); const range = ss.getRange('A1:C20'); const filter = range.getFilter(); // Gets the filter criteria applied to column B, then gets the hidden values. const filterCriteria = filter.getColumnFilterCriteria(2).getHiddenValues(); // Logs the hidden values. console.log(filterCriteria);
方法
| 方法 | 返回类型 | 简介 |
|---|---|---|
copy() | Filter | 复制此过滤条件,并创建一个可应用于其他过滤条件的条件构建器。 |
get | Boolean | 返回相应条件的布尔值类型,例如 CELL_EMPTY。 |
get | Object[] | 返回布尔值条件的一组实参。 |
get | String[] | 返回过滤条件隐藏的值。 |
get | Color|null | 返回用作过滤条件的背景颜色。 |
get | Color|null | 返回用作过滤条件的前景色。 |
get | String[] | 返回透视表过滤器显示的值。 |
详细文档
copy()
复制此过滤条件并创建一个可应用于其他过滤条件的条件构建器。
您可以将此方法与任何类型的过滤条件搭配使用。如果您使用的是工作表过滤条件,则可以将条件复制到其他列。
const ss = SpreadsheetApp.getActiveSheet(); const filter = ss.getFilter(); // Makes a copy of the filter criteria applied to column C. const criteria = 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);
返回
Filter - 基于相应过滤条件的过滤条件构建器。
get Criteria Type()
返回相应条件的布尔值类型,例如 CELL_EMPTY。如需了解布尔值条件的类型,请参阅 Boolean 枚举。
人们通常使用此方法向过滤器添加布尔值条件,而不会替换现有条件。
- 如需获取条件的实参,请使用
get。Criteria Values() - 如需使用条件类型和条件值来创建或修改过滤条件,请参阅
Filter。Criteria Builder.withCriteria(criteria, args)
此方法适用于任何类型的过滤条件。如果过滤条件不是布尔值条件,则返回 null。
const ss = SpreadsheetApp.getActiveSheet(); // Gets the filter on the active sheet. const filter = ss.getFilter(); // Gets the criteria type and returns a string representing the criteria type // object. const criteriaType = filter.getColumnFilterCriteria(2).getCriteriaType().toString(); // Logs the criteria type. console.log(criteriaType);
返回
Boolean - 布尔值条件的类型,如果相应条件不是布尔值条件,则为 null。
get Criteria Values()
返回布尔值条件的实参数组。某些布尔值条件类型没有实参,并返回一个空数组,例如 CELL_NOT_EMPTY。
人们通常使用此方法向过滤器添加布尔值条件,而不会替换现有条件。
- 如需获取布尔值条件类型,请使用
get。Criteria Type() - 如需使用条件类型和条件值创建或修改过滤条件,请参阅
Filter。Criteria Builder.withCriteria(criteria, args) 此方法适用于任何类型的过滤条件。
const ss = SpreadsheetApp.getActiveSheet(); const filter = 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. const criteriaValues = filter.getColumnFilterCriteria(2).getCriteriaValues(); console.log(criteriaValues);
返回
Object[]- 适合布尔值条件类型的实参数组。实参的数量及其类型与Filter类的相应Criteria Builder when...()方法相匹配。
get Hidden Values()
返回过滤条件隐藏的值。
将此条件与Grid工作表(默认工作表类型)中的过滤条件搭配使用。
如果您针对其他类型的过滤条件调用此方法,则返回 null。
const ss = SpreadsheetApp.getActiveSheet(); const range = ss.getRange('A1:C20'); const filter = range.getFilter(); // Gets the filter criteria applied to column B, then gets the hidden values. const filterCriteria = filter.getColumnFilterCriteria(2).getHiddenValues(); // Logs the hidden values. console.log(filterCriteria);
返回
String[] - 过滤条件隐藏的值的数组。
get Visible Background Color()
返回用作过滤条件的背景颜色。具有此背景色的单元格仍保持可见。
将此条件与Grid工作表(默认工作表类型)中的过滤条件搭配使用。
如果您针对其他类型的过滤条件调用此方法,则返回 null。
const ss = SpreadsheetApp.getActiveSheet(); const range = ss.getRange('A1:C20'); // Logs the background color that column B is filtered by as a hexadecimal // string. const filter = range.getFilter(); const color = filter.getColumnFilterCriteria(2) .getVisibleBackgroundColor() .asRgbColor() .asHexString(); console.log(color);
返回
Color|null - 用作过滤条件的背景颜色。
get Visible Foreground Color()
返回用作过滤条件的前景颜色。具有此前景色的单元格仍保持可见。
将此条件与Grid工作表(默认工作表类型)中的过滤条件搭配使用。
如果您针对其他类型的过滤条件调用此方法,则返回 null。
const ss = SpreadsheetApp.getActiveSheet(); const range = ss.getRange('A1:C20'); // Logs the foreground color that column B is filtered by as a hexadecimal // string. const filter = range.getFilter(); const color = filter.getColumnFilterCriteria(2) .getVisibleForegroundColor() .asRgbColor() .asHexString(); console.log(color);
返回
Color|null - 用作过滤条件的前景色。
get Visible Values()
返回透视表过滤器显示的值。
此条件仅适用于未连接到数据库的数据透视表上的过滤条件。对于其他类型的过滤条件,则返回空数组。
const ss = SpreadsheetApp.getActiveSheet(); // Gets the first pivot table on the sheet, then gets the visible values of its // first filter. const pivotTable = ss.getPivotTables()[0]; const pivotFilterValues = pivotTable.getFilters()[0].getFilterCriteria().getVisibleValues(); // Logs the visible values. console.log(pivotFilterValues);
返回
String[] - 数据透视表过滤条件显示的值的数组。