Excel.PivotTable class

表示 Excel 数据透视表。Represents an Excel PivotTable.

[API 集: ExcelApi 1.3][ API set: ExcelApi 1.3 ]

Extends

属性

columnHierarchies

数据透视表的列透视层级结构。The Column Pivot Hierarchies of the PivotTable.

[API 集: ExcelApi 1.8][ API set: ExcelApi 1.8 ]

context

与该对象关联的请求上下文。The request context associated with the object. 这会将加载项的进程连接到 Office 主机应用程序的进程。This connects the add-in's process to the Office host application's process.

dataHierarchies

数据透视表的数据透视层级结构。The Data Pivot Hierarchies of the PivotTable.

[API 集: ExcelApi 1.8][ API set: ExcelApi 1.8 ]

enableDataValueEditing

指定数据透视表是否允许用户对数据正文中的值进行编辑。Specifies whether the PivotTable allows values in the data body to be edited by the user.

[API 集: ExcelApi 1.9][ API set: ExcelApi 1.9 ]

filterHierarchies

数据透视表的筛选器透视层级结构。The Filter Pivot Hierarchies of the PivotTable.

[API 集: ExcelApi 1.8][ API set: ExcelApi 1.8 ]

hierarchies

数据透视表的透视层级结构。The Pivot Hierarchies of the PivotTable.

[API 集: ExcelApi 1.8][ API set: ExcelApi 1.8 ]

id

数据透视表的 ID。Id of the PivotTable. 只读。Read-only.

[API 集: ExcelApi 1.5][ API set: ExcelApi 1.5 ]

layout

PivotLayout,用于说明数据透视表的布局和可视化结构。The PivotLayout describing the layout and visual structure of the PivotTable.

[API 集: ExcelApi 1.8][ API set: ExcelApi 1.8 ]

name

PivotTable 对象的名称。Name of the PivotTable.

[API 集: ExcelApi 1.3][ API set: ExcelApi 1.3 ]

rowHierarchies

数据透视表的行透视层级结构。The Row Pivot Hierarchies of the PivotTable.

[API 集: ExcelApi 1.8][ API set: ExcelApi 1.8 ]

useCustomSortLists

指定在排序时, 数据透视表是否使用自定义列表。Specifies whether the PivotTable uses custom lists when sorting.

[API 集: ExcelApi 1.9][ API set: ExcelApi 1.9 ]

worksheet

包含当前 PivotTable 对象的工作表。The worksheet containing the current PivotTable.

[API 集: ExcelApi 1.3][ API set: ExcelApi 1.3 ]

方法

delete()

删除 PivotTable 对象。Deletes the PivotTable.

[API 集: ExcelApi 1.8][ API set: ExcelApi 1.8 ]

load(option)

将命令加入队列以加载对象的指定属性。Queues up a command to load the specified properties of the object. 在读取属性之前,你必须调用“context.sync()”。You must call "context.sync()" before reading the properties.

load(propertyNames)

将命令加入队列以加载对象的指定属性。Queues up a command to load the specified properties of the object. 在读取属性context.sync()之前, 必须先调用。You must call context.sync() before reading the properties.

load(propertyNamesAndPaths)

将命令加入队列以加载对象的指定属性。Queues up a command to load the specified properties of the object. 在读取属性context.sync()之前, 必须先调用。You must call context.sync() before reading the properties.

refresh()

刷新 PivotTable 对象。Refreshes the PivotTable.

[API 集: ExcelApi 1.3][ API set: ExcelApi 1.3 ]

set(properties, options)

同时设置一个对象的多个属性。Sets multiple properties of an object at the same time. 您可以传递具有相应属性的纯对象或相同类型的其他 API 对象。You can pass either a plain object with the appropriate properties, or another API object of the same type.

set(properties)

基于现有加载的对象同时设置该对象的多个属性。Sets multiple properties on the object at the same time, based on an existing loaded object.

toJSON()

重写 JavaScript toJSON()方法, 以便在将 API 对象传递到时提供更有用的输出JSON.stringify()Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify反过来, 也会调用传递toJSON给它的对象的方法。尽管原始的 Excel 数据透视表对象是 API 对象, 但toJSON方法返回一个简单的 JavaScript 对象 (类型为Excel.Interfaces.PivotTableData, in turn, calls the toJSON method of the object that is passed to it.) Whereas the original Excel.PivotTable object is an API object, the toJSON method returns a plain JavaScript object (typed as Excel.Interfaces.PivotTableData), 其中包含原始对象中所有已加载的子属性的浅表副本。) that contains shallow copies of any loaded child properties from the original object.

属性详细信息

columnHierarchies

数据透视表的列透视层级结构。The Column Pivot Hierarchies of the PivotTable.

[API 集: ExcelApi 1.8][ API set: ExcelApi 1.8 ]

readonly columnHierarchies: Excel.RowColumnPivotHierarchyCollection;

属性值

示例Examples

await Excel.run(async (context) => {
    const pivotTable = context.workbook.worksheets.getActiveWorksheet().pivotTables.getItem("Farm Sales");       

    // check if the PivotTable already has a column
    const column = pivotTable.columnHierarchies.getItemOrNullObject("Farm");
    column.load("id");
    await context.sync();

    if (column.isNullObject) {
        // ading the farm column to the column hierarchy automatically removes it from the row hierarchy
        pivotTable.columnHierarchies.add(pivotTable.hierarchies.getItem("Farm"));
    } else {
        pivotTable.columnHierarchies.remove(column);
    }

    await context.sync();
});

context

与该对象关联的请求上下文。The request context associated with the object. 这会将加载项的进程连接到 Office 主机应用程序的进程。This connects the add-in's process to the Office host application's process.

context: RequestContext;

属性值

RequestContext

dataHierarchies

数据透视表的数据透视层级结构。The Data Pivot Hierarchies of the PivotTable.

[API 集: ExcelApi 1.8][ API set: ExcelApi 1.8 ]

readonly dataHierarchies: Excel.DataPivotHierarchyCollection;

属性值

示例Examples

await Excel.run(async (context) => {
    const pivotTable = context.workbook.worksheets.getActiveWorksheet().pivotTables.getItem("Farm Sales");
    pivotTable.dataHierarchies.add(pivotTable.hierarchies.getItem("Crates Sold at Farm"));
    pivotTable.dataHierarchies.add(pivotTable.hierarchies.getItem("Crates Sold Wholesale"));

    await context.sync();
});

enableDataValueEditing

指定数据透视表是否允许用户对数据正文中的值进行编辑。Specifies whether the PivotTable allows values in the data body to be edited by the user.

[API 集: ExcelApi 1.9][ API set: ExcelApi 1.9 ]

enableDataValueEditing: boolean;

属性值

boolean

filterHierarchies

数据透视表的筛选器透视层级结构。The Filter Pivot Hierarchies of the PivotTable.

[API 集: ExcelApi 1.8][ API set: ExcelApi 1.8 ]

readonly filterHierarchies: Excel.FilterPivotHierarchyCollection;

属性值

示例Examples

async function filter(functionType: Excel.AggregationFunction) {
    await Excel.run(async (context) => {
        const pivotTable = context.workbook.worksheets.getActiveWorksheet().pivotTables.getItem("Farm Sales");
        const filters = pivotTable.filterHierarchies;
        const filter = filters.getItemOrNullObject("Classification");
        filter.load();
        await context.sync();

        // add the Classification hierarchy to the filter, if it's not already there
        if (filter.isNullObject) {
            filters.add(pivotTable.hierarchies.getItem("Classification"));
            await context.sync();
        }
    });
}

hierarchies

数据透视表的透视层级结构。The Pivot Hierarchies of the PivotTable.

[API 集: ExcelApi 1.8][ API set: ExcelApi 1.8 ]

readonly hierarchies: Excel.PivotHierarchyCollection;

属性值

id

数据透视表的 ID。Id of the PivotTable. 只读。Read-only.

[API 集: ExcelApi 1.5][ API set: ExcelApi 1.5 ]

readonly id: string;

属性值

string

layout

PivotLayout,用于说明数据透视表的布局和可视化结构。The PivotLayout describing the layout and visual structure of the PivotTable.

[API 集: ExcelApi 1.8][ API set: ExcelApi 1.8 ]

readonly layout: Excel.PivotLayout;

属性值

示例Examples

await Excel.run(async (context) => {
    const pivotTable = context.workbook.worksheets.getActiveWorksheet().pivotTables.getItem("Farm Sales");
    pivotTable.layout.load("layoutType");
    await context.sync();

    // cycle between the three layout types
    if (pivotTable.layout.layoutType === "Compact") {
        pivotTable.layout.layoutType = "Outline";
    } else if (pivotTable.layout.layoutType === "Outline") {
        pivotTable.layout.layoutType = "Tabular";
    } else {
        pivotTable.layout.layoutType = "Compact";
    }
    await context.sync();
    console.log("Pivot layout is now " + pivotTable.layout.layoutType);
});

name

PivotTable 对象的名称。Name of the PivotTable.

[API 集: ExcelApi 1.3][ API set: ExcelApi 1.3 ]

name: string;

属性值

string

rowHierarchies

数据透视表的行透视层级结构。The Row Pivot Hierarchies of the PivotTable.

[API 集: ExcelApi 1.8][ API set: ExcelApi 1.8 ]

readonly rowHierarchies: Excel.RowColumnPivotHierarchyCollection;

属性值

useCustomSortLists

指定在排序时, 数据透视表是否使用自定义列表。Specifies whether the PivotTable uses custom lists when sorting.

[API 集: ExcelApi 1.9][ API set: ExcelApi 1.9 ]

useCustomSortLists: boolean;

属性值

boolean

worksheet

包含当前 PivotTable 对象的工作表。The worksheet containing the current PivotTable.

[API 集: ExcelApi 1.3][ API set: ExcelApi 1.3 ]

readonly worksheet: Excel.Worksheet;

属性值

方法详细信息

delete()

删除 PivotTable 对象。Deletes the PivotTable.

[API 集: ExcelApi 1.8][ API set: ExcelApi 1.8 ]

delete(): void;

返回

void

示例Examples

await Excel.run(async (context) => {
    context.workbook.worksheets.getItem("Pivot").pivotTables.getItem("Farm Sales").delete();

    await context.sync();
});

load(option)

将命令加入队列以加载对象的指定属性。Queues up a command to load the specified properties of the object. 在读取属性之前,你必须调用“context.sync()”。You must call "context.sync()" before reading the properties.

load(option?: Excel.Interfaces.PivotTableLoadOptions): Excel.PivotTable;

参数

返回

注解

除了此签名之外, 此方法还具有以下签名:In addition to this signature, this method has the following signatures:

load(option?: string | string[]): Excel.PivotTable-Where 选项是一个逗号分隔的字符串或一个字符串数组, 用于指定要加载的属性。load(option?: string | string[]): Excel.PivotTable - Where option is a comma-delimited string or an array of strings that specify the properties to load.

load(option?: { select?: string; expand?: string; }): Excel.PivotTable-Where 选项。选择是一个用逗号分隔的字符串, 用于指定要加载的属性和选项。 expand 是一个用逗号分隔的字符串, 用于指定要加载的导航属性。load(option?: { select?: string; expand?: string; }): Excel.PivotTable - Where option.select is a comma-delimited string that specifies the properties to load, and options.expand is a comma-delimited string that specifies the navigation properties to load.

load(option?: { select?: string; expand?: string; top?: number; skip?: number }): Excel.PivotTable-仅适用于集合类型。load(option?: { select?: string; expand?: string; top?: number; skip?: number }): Excel.PivotTable - Only available on collection types. 它类似于前面的签名。It is similar to the preceding signature. Top 指定结果中可包含的集合项的最大数量。Option.top specifies the maximum number of collection items that can be included in the result. 选项。 skip 指定要跳过且不包含在结果中的项目数。Option.skip specifies the number of items that are to be skipped and not included in the result. 如果指定了 top 选项, 则在跳过指定数目的项目后将启动结果集。If option.top is specified, the result set will start after skipping the specified number of items.

load(propertyNames)

将命令加入队列以加载对象的指定属性。Queues up a command to load the specified properties of the object. 在读取属性context.sync()之前, 必须先调用。You must call context.sync() before reading the properties.

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

参数

propertyNames
string | string[]

一个以逗号分隔的字符串或字符串数组, 用于指定要加载的属性。A comma-delimited string or an array of strings that specify the properties to load.

返回

load(propertyNamesAndPaths)

将命令加入队列以加载对象的指定属性。Queues up a command to load the specified properties of the object. 在读取属性context.sync()之前, 必须先调用。You must call context.sync() before reading the properties.

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

参数

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

其中, propertyNamesAndPaths 是用逗号分隔的字符串, 用于指定要加载的属性和 propertyNamesAndPaths。扩展是一个用逗号分隔的字符串, 用于指定要加载的导航属性。Where propertyNamesAndPaths.select is a comma-delimited string that specifies the properties to load, and propertyNamesAndPaths.expand is a comma-delimited string that specifies the navigation properties to load.

返回

refresh()

刷新 PivotTable 对象。Refreshes the PivotTable.

[API 集: ExcelApi 1.3][ API set: ExcelApi 1.3 ]

refresh(): void;

返回

void

set(properties, options)

同时设置一个对象的多个属性。Sets multiple properties of an object at the same time. 您可以传递具有相应属性的纯对象或相同类型的其他 API 对象。You can pass either a plain object with the appropriate properties, or another API object of the same type.

set(properties: Interfaces.PivotTableUpdateData, options?: OfficeExtension.UpdateOptions): void;

参数

properties
Interfaces.PivotTableUpdateData

一个 JavaScript 对象, 其属性的结构 isomorphically 为在其上调用方法的对象的属性。A JavaScript object with properties that are structured isomorphically to the properties of the object on which the method is called.

options
OfficeExtension.UpdateOptions

提供一个选项, 以在 properties 对象尝试设置任何只读属性时禁止显示错误。Provides an option to suppress errors if the properties object tries to set any read-only properties.

返回

void

注解

此方法具有以下附加签名:This method has the following additional signature:

set(properties: Excel.PivotTable): void

set(properties)

基于现有加载的对象同时设置该对象的多个属性。Sets multiple properties on the object at the same time, based on an existing loaded object.

set(properties: Excel.PivotTable): void;

参数

properties
Excel.PivotTable

返回

void

toJSON()

重写 JavaScript toJSON()方法, 以便在将 API 对象传递到时提供更有用的输出JSON.stringify()Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify反过来, 也会调用传递toJSON给它的对象的方法。尽管原始的 Excel 数据透视表对象是 API 对象, 但toJSON方法返回一个简单的 JavaScript 对象 (类型为Excel.Interfaces.PivotTableData, in turn, calls the toJSON method of the object that is passed to it.) Whereas the original Excel.PivotTable object is an API object, the toJSON method returns a plain JavaScript object (typed as Excel.Interfaces.PivotTableData), 其中包含原始对象中所有已加载的子属性的浅表副本。) that contains shallow copies of any loaded child properties from the original object.

toJSON(): Excel.Interfaces.PivotTableData;

返回