Class Filter

过滤

使用该类修改 Grid 工作表(工作表的默认类型)上的现有过滤条件。网格工作表是指包含数据未连接到数据库的常规工作表。

如果工作表中还没有过滤条件,请使用 Range.createFilter() 创建一个。

如需使用此类,您必须先使用 Range.getFilter()Sheet.getFilter() 访问网格工作表过滤条件。

常见的使用情形

移除过滤条件

以下示例会获取有效工作表中的过滤器并将其移除。
let ss = SpreadsheetApp.getActiveSheet();
let filter = ss.getFilter();
// Removes the filter from the active sheet.
filter.remove();

获取过滤条件适用的范围

以下示例会获取当前工作表中的过滤条件,然后使用该类中的 getRange() 方法记录该过滤条件所适用的范围。
let ss = SpreadsheetApp.getActiveSheet();
// Gets the existing filter on the active sheet.
let filter = ss.getFilter();
// Logs the range that the filter applies to in A1 notation.
console.log(filter.getRange().getA1Notation());

方法

方法返回类型简介
getColumnFilterCriteria(columnPosition)FilterCriteria获取指定列的过滤条件,如果列未应用过滤条件,则为 null
getRange()Range获取此过滤条件适用的范围。
remove()void移除此过滤条件。
removeColumnFilterCriteria(columnPosition)Filter从指定列中移除过滤条件。
setColumnFilterCriteria(columnPosition, filterCriteria)Filter设置指定列的过滤条件。
sort(columnPosition, ascending)Filter按指定列对过滤范围进行排序,但排除此过滤条件所适用的范围的第一行(标题行)。

详细文档

getColumnFilterCriteria(columnPosition)

获取指定列的过滤条件;如果列未应用过滤条件,则返回 null

如需获取有关过滤条件的更多详细信息,请将此方法与 FilterCriteria 类中的方法串联起来。

let ss = SpreadsheetApp.getActiveSheet();
let filter = ss.getFilter();
  // Gets the filter criteria applied to column B of the active sheet
  // and logs the hidden values.
let filterCriteria = filter.getColumnFilterCriteria(2).getHiddenValues();
console.log(filterCriteria);

参数

名称类型说明
columnPositionInteger从 1 开始计数的列的位置。例如,B 列的索引为 2。

弃踢回攻

FilterCriteria - 过滤条件。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

getRange()

获取此过滤条件适用的范围。

// Gets the existing filter on the active sheet.
let ss = SpreadsheetApp.getActiveSheet();
let filter = ss.getFilter();
// Logs the range that the filter applies to in A1 notation.
console.log(filter.getRange().getA1Notation());

弃踢回攻

Range - 过滤器的范围。如需获取采用 A1 表示法表示的范围,请将此方法与 Range.getA1Notation() 链接起来。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

remove()

移除此过滤器。

// Removes the filter from the active sheet.
let ss = SpreadsheetApp.getActiveSheet();
let filter = ss.getFilter();
filter.remove();

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

removeColumnFilterCriteria(columnPosition)

从指定列中移除过滤条件。

// Removes the filter criteria from column B.
let ss = SpreadsheetApp.getActiveSheet();
let filter = ss.getFilter();
filter.removeColumnFilterCriteria(2);

参数

名称类型说明
columnPositionInteger从 1 开始计数的列的位置。例如,B 列的索引为 2。

弃踢回攻

Filter - 用于链接的过滤条件。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

setColumnFilterCriteria(columnPosition, filterCriteria)

设置指定列的过滤条件。首先,使用 SpreadsheetApp.newFilterCriteria() 创建过滤条件构建器。然后,使用 FilterCriteriaBuilder 类向构建器添加条件。构建条件后,请将其设置为此方法的 filterCriteria 参数。

let ss = SpreadsheetApp.getActiveSheet();
let filter = ss.getFilter();
// Builds the filter criteria to use as a parameter for setColumnFilterCriteria.
const criteria = SpreadsheetApp.newFilterCriteria()
                             .setHiddenValues(["Hello", "World"])
                             .build();
// Sets the filter criteria for column C.
filter.setColumnFilterCriteria(3, criteria);

参数

名称类型说明
columnPositionInteger从 1 开始计数的列的位置。例如,B 列的索引为 2。
filterCriteriaFilterCriteria要设置的过滤条件。如果将条件设置为 null,则将从指定的列中移除过滤条件。您也可以使用 removeColumnFilterCriteria(columnPosition)

弃踢回攻

Filter - 用于链接的过滤条件。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

sort(columnPosition, ascending)

按指定列对过滤范围进行排序,但排除此过滤条件适用的范围的第一行(标题行)。

// Gets the existing filter and sorts it by column B in ascending order.
let ss = SpreadsheetApp.getActiveSheet();
let filter = ss.getFilter();
filter.sort(2, true);

参数

名称类型说明
columnPositionInteger从 1 开始计数的列的位置。例如,B 列的索引为 2。
ascendingBoolean如果为 true,则按升序对过滤范围进行排序;如果为 false,则对已过滤的范围按降序排序。

弃踢回攻

Filter - 用于链接的过滤条件。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets