Excel.AutoFilter class

代表 AutoFilter 对象。 自动筛选根据单元格内容将 Excel 列中的值转换为特定筛选器。

扩展

属性

context

与对象关联的请求上下文。 这将加载项的进程连接到 Office 主机应用程序的进程。

criteria

在自动筛选区域中保留所有筛选条件的数组。

enabled

指定是否启用自动筛选。

isDataFiltered

指定自动筛选是否具有筛选条件。

方法

apply(range, columnIndex, criteria)

将自动筛选器应用于区域。 如果指定了列索引和筛选条件,则筛选列。

clearColumnCriteria(columnIndex)

清除自动筛选的列筛选条件。

clearCriteria()

清除自动筛选器的筛选条件和排序状态。

getRange()

返回 Range 表示自动筛选应用到的范围的对象。

getRangeOrNullObject()

返回 Range 表示自动筛选应用到的范围的对象。 如果没有 Range 与 AutoFilter 关联的对象,则此方法返回其 isNullObject 属性设置为 true的对象。 有关详细信息,请参阅 *OrNullObject 方法和属性

load(options)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(propertyNames)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(propertyNamesAndPaths)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

reapply()

应用当前在范围内的指定自动筛选对象。

remove()

删除区域的自动筛选。

toJSON()

覆盖 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 (JSON.stringify反过来调用 toJSON 传递给它的对象的方法。) 虽然原始 Excel.AutoFilter 对象是 API 对象, toJSON 但该方法返回一个纯 JavaScript 对象, (类型化为 Excel.Interfaces.AutoFilterData) ,其中包含从原始对象加载的任何子属性的浅层副本。

属性详细信息

context

与对象关联的请求上下文。 这将加载项的进程连接到 Office 主机应用程序的进程。

context: RequestContext;

属性值

criteria

在自动筛选区域中保留所有筛选条件的数组。

readonly criteria: Excel.FilterCriteria[];

属性值

注解

API 集:ExcelApi 1.9

enabled

指定是否启用自动筛选。

readonly enabled: boolean;

属性值

boolean

注解

API 集:ExcelApi 1.9

isDataFiltered

指定自动筛选是否具有筛选条件。

readonly isDataFiltered: boolean;

属性值

boolean

注解

API 集:ExcelApi 1.9

方法详细信息

apply(range, columnIndex, criteria)

将自动筛选器应用于区域。 如果指定了列索引和筛选条件,则筛选列。

apply(range: Range | string, columnIndex?: number, criteria?: Excel.FilterCriteria): void;

参数

range

Excel.Range | string

自动筛选将适用的范围。

columnIndex

number

应用自动筛选的从零开始的列索引。

criteria
Excel.FilterCriteria

筛选条件。

返回

void

注解

API 集:ExcelApi 1.9

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/54-worksheet/worksheet-auto-filter.yaml

// This function adds a percentage AutoFilter to the active worksheet 
// and applies the filter to a column of the used range.
await Excel.run(async (context) => {
    // Retrieve the active worksheet and the used range on that worksheet.
    const sheet = context.workbook.worksheets.getActiveWorksheet();
    const farmData = sheet.getUsedRange();

    // Add a filter that will only show the rows with the top 50% of values in column 3.
    sheet.autoFilter.apply(farmData, 3, {
        criterion1: "50",
        filterOn: Excel.FilterOn.topPercent
    });

    await context.sync();
});

clearColumnCriteria(columnIndex)

清除自动筛选的列筛选条件。

clearColumnCriteria(columnIndex: number): void;

参数

columnIndex

number

从零开始的列索引,表示需要清除哪个列筛选器。 如果索引值不受支持 (例如,如果该值是负数,或者该值大于) 区域中的可用列数, InvalidArgument 则将引发错误。

返回

void

注解

API 集:ExcelApi 1.14

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/54-worksheet/worksheet-auto-filter.yaml

// This function clears the AutoFilter setting from one column.
await Excel.run(async (context) => {
    // Retrieve the active worksheet.
    const sheet = context.workbook.worksheets.getActiveWorksheet();

    // Clear the filter from only column 3.
    sheet.autoFilter.clearColumnCriteria(3);
    await context.sync();
});

clearCriteria()

清除自动筛选器的筛选条件和排序状态。

clearCriteria(): void;

返回

void

注解

API 集:ExcelApi 1.9

getRange()

返回 Range 表示自动筛选应用到的范围的对象。

getRange(): Excel.Range;

返回

注解

API 集:ExcelApi 1.9

getRangeOrNullObject()

返回 Range 表示自动筛选应用到的范围的对象。 如果没有 Range 与 AutoFilter 关联的对象,则此方法返回其 isNullObject 属性设置为 true的对象。 有关详细信息,请参阅 *OrNullObject 方法和属性

getRangeOrNullObject(): Excel.Range;

返回

注解

API 集:ExcelApi 1.9

load(options)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(options?: Excel.Interfaces.AutoFilterLoadOptions): Excel.AutoFilter;

参数

options
Excel.Interfaces.AutoFilterLoadOptions

为要加载的对象属性提供选项。

返回

load(propertyNames)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(propertyNames?: string | string[]): Excel.AutoFilter;

参数

propertyNames

string | string[]

指定要加载的属性的逗号分隔的字符串或字符串数组。

返回

load(propertyNamesAndPaths)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(propertyNamesAndPaths?: {
            select?: string;
            expand?: string;
        }): Excel.AutoFilter;

参数

propertyNamesAndPaths

{ select?: string; expand?: string; }

propertyNamesAndPaths.select 是指定要加载的属性的逗号分隔字符串,以及 propertyNamesAndPaths.expand 指定要加载的导航属性的逗号分隔字符串。

返回

reapply()

应用当前在范围内的指定自动筛选对象。

reapply(): void;

返回

void

注解

API 集:ExcelApi 1.9

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/54-worksheet/worksheet-auto-filter.yaml

// This function refreshes the AutoFilter to ensure that changes are captured.
await Excel.run(async (context) => {
    // Retrieve the active worksheet.
    const sheet = context.workbook.worksheets.getActiveWorksheet();

    // Reapply the filter to capture changes.
    sheet.autoFilter.reapply();
    await context.sync();
});

remove()

删除区域的自动筛选。

remove(): void;

返回

void

注解

API 集:ExcelApi 1.9

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/54-worksheet/worksheet-auto-filter.yaml

// This function removes all AutoFilters from the active worksheet.
await Excel.run(async (context) => {
    // Retrieve the active worksheet.
    const sheet = context.workbook.worksheets.getActiveWorksheet();

    // Remove all filters.
    sheet.autoFilter.remove();
    await context.sync();
});

toJSON()

覆盖 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 (JSON.stringify反过来调用 toJSON 传递给它的对象的方法。) 虽然原始 Excel.AutoFilter 对象是 API 对象, toJSON 但该方法返回一个纯 JavaScript 对象, (类型化为 Excel.Interfaces.AutoFilterData) ,其中包含从原始对象加载的任何子属性的浅层副本。

toJSON(): Excel.Interfaces.AutoFilterData;

返回