Excel.PivotTable class

表示 Excel 数据透视表。 若要了解有关数据透视表对象模型的详细信息,请阅读 使用 Excel JavaScript API 处理数据透视表。

扩展

属性

allowMultipleFiltersPerField

指定数据透视表是否允许对表中的给定数据透视字段应用多个数据透视筛选器。

autoRefresh

指定数据透视表是否在源数据发生更改时自动刷新。 对应于 UI 中的“自动刷新”设置。

columnHierarchies

数据透视表的列透视层级结构。

context

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

dataHierarchies

数据透视表的数据透视层级结构。

enableDataValueEditing

指定数据透视表是否允许用户编辑数据正文中的值。

filterHierarchies

数据透视表的筛选器透视层级结构。

hierarchies

数据透视表的透视层级结构。

id

数据透视表的 ID。

layout

PivotLayout,用于说明数据透视表的布局和可视化结构。

name

PivotTable 对象的名称。

refreshOnOpen

指定打开工作簿时是否刷新数据透视表。 对应于 UI 中的“加载时刷新”设置。

rowHierarchies

数据透视表的行透视层级结构。

useCustomSortLists

指定数据透视表在排序时是否使用自定义列表。

worksheet

包含当前 PivotTable 对象的工作表。

方法

delete()

删除 PivotTable 对象。

getDataSourceString()

返回数据透视表的数据源的字符串表示形式。 此方法当前支持表对象和区域对象的字符串表示形式。 否则,它将返回一个空字符串。

getDataSourceType()

获取数据透视表的数据源的类型。

load(options)

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

load(propertyNames)

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

load(propertyNamesAndPaths)

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

refresh()

刷新 PivotTable 对象。

set(properties, options)

同时设置对象的多个属性。 可以传递具有相应属性的纯文本对象,也可以传递另一个相同类型的 API 对象。

set(properties)

基于现有加载的对象,同时在对象上设置多个属性。

toJSON()

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

属性详细信息

allowMultipleFiltersPerField

指定数据透视表是否允许对表中的给定数据透视字段应用多个数据透视筛选器。

allowMultipleFiltersPerField: boolean;

属性值

boolean

注解

API 集:ExcelApi 1.12

autoRefresh

注意

此 API 以预览状态提供给开发者,可能根据我们收到的反馈更改。 请勿在生产环境中使用此 API。

指定数据透视表是否在源数据发生更改时自动刷新。 对应于 UI 中的“自动刷新”设置。

autoRefresh: boolean;

属性值

boolean

注解

API 集:ExcelApi BETA (仅预览版)

columnHierarchies

数据透视表的列透视层级结构。

readonly columnHierarchies: Excel.RowColumnPivotHierarchyCollection;

属性值

注解

API 集:ExcelApi 1.8

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/38-pivottable/pivottable-create-and-modify.yaml

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) {
        // Adding 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

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

context: RequestContext;

属性值

dataHierarchies

数据透视表的数据透视层级结构。

readonly dataHierarchies: Excel.DataPivotHierarchyCollection;

属性值

注解

API 集:ExcelApi 1.8

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/38-pivottable/pivottable-create-and-modify.yaml

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

指定数据透视表是否允许用户编辑数据正文中的值。

enableDataValueEditing: boolean;

属性值

boolean

注解

API 集:ExcelApi 1.9

filterHierarchies

数据透视表的筛选器透视层级结构。

readonly filterHierarchies: Excel.FilterPivotHierarchyCollection;

属性值

注解

API 集:ExcelApi 1.8

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/38-pivottable/pivottable-filters-and-summaries.yaml

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

数据透视表的透视层级结构。

readonly hierarchies: Excel.PivotHierarchyCollection;

属性值

注解

API 集:ExcelApi 1.8

id

数据透视表的 ID。

readonly id: string;

属性值

string

注解

API 集:ExcelApi 1.5

layout

PivotLayout,用于说明数据透视表的布局和可视化结构。

readonly layout: Excel.PivotLayout;

属性值

注解

API 集:ExcelApi 1.8

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/38-pivottable/pivottable-create-and-modify.yaml

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: string;

属性值

string

注解

API 集:ExcelApi 1.3

refreshOnOpen

指定打开工作簿时是否刷新数据透视表。 对应于 UI 中的“加载时刷新”设置。

refreshOnOpen: boolean;

属性值

boolean

注解

API 集:ExcelApi 1.13

rowHierarchies

数据透视表的行透视层级结构。

readonly rowHierarchies: Excel.RowColumnPivotHierarchyCollection;

属性值

注解

API 集:ExcelApi 1.8

useCustomSortLists

指定数据透视表在排序时是否使用自定义列表。

useCustomSortLists: boolean;

属性值

boolean

注解

API 集:ExcelApi 1.9

worksheet

包含当前 PivotTable 对象的工作表。

readonly worksheet: Excel.Worksheet;

属性值

注解

API 集:ExcelApi 1.3

方法详细信息

delete()

删除 PivotTable 对象。

delete(): void;

返回

void

注解

API 集:ExcelApi 1.8

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/38-pivottable/pivottable-create-and-modify.yaml

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

    await context.sync();
});

getDataSourceString()

返回数据透视表的数据源的字符串表示形式。 此方法当前支持表对象和区域对象的字符串表示形式。 否则,它将返回一个空字符串。

getDataSourceString(): OfficeExtension.ClientResult<string>;

返回

注解

API 集:ExcelApi 1.15

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/38-pivottable/pivottable-source-data.yaml

// This function logs information about the data source of a PivotTable.
await Excel.run(async (context) => {
  const worksheet = context.workbook.worksheets.getItem("TotalPivot");
  const pivotTable = worksheet.pivotTables.getItem("All Farm Sales");

  // Retrieve the type and string representation of the data source of the PivotTable.
  const pivotTableDataSourceType = pivotTable.getDataSourceType();
  const pivotTableDataSourceString = pivotTable.getDataSourceString();
  await context.sync();

  // Log the data source information.
  console.log("Data source: " + pivotTableDataSourceString.value);
  console.log("Source type: " + pivotTableDataSourceType.value);
});

getDataSourceType()

获取数据透视表的数据源的类型。

getDataSourceType(): OfficeExtension.ClientResult<Excel.DataSourceType>;

返回

注解

API 集:ExcelApi 1.15

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/38-pivottable/pivottable-source-data.yaml

// This function logs information about the data source of a PivotTable.
await Excel.run(async (context) => {
  const worksheet = context.workbook.worksheets.getItem("TotalPivot");
  const pivotTable = worksheet.pivotTables.getItem("All Farm Sales");

  // Retrieve the type and string representation of the data source of the PivotTable.
  const pivotTableDataSourceType = pivotTable.getDataSourceType();
  const pivotTableDataSourceString = pivotTable.getDataSourceString();
  await context.sync();

  // Log the data source information.
  console.log("Data source: " + pivotTableDataSourceString.value);
  console.log("Source type: " + pivotTableDataSourceType.value);
});

load(options)

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

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

参数

options
Excel.Interfaces.PivotTableLoadOptions

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

返回

load(propertyNames)

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

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

参数

propertyNames

string | string[]

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

返回

load(propertyNamesAndPaths)

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

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

参数

propertyNamesAndPaths

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

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

返回

refresh()

刷新 PivotTable 对象。

refresh(): void;

返回

void

注解

API 集:ExcelApi 1.3

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/38-pivottable/pivottable-refresh.yaml

// This function refreshes the "Farm Sales" PivotTable,
// which updates the PivotTable with changes made to the source table.
await Excel.run(async (context) => {
  const pivotTable = context.workbook.pivotTables.getItem("Farm Sales");
  pivotTable.refresh();
  await context.sync();
});

set(properties, options)

同时设置对象的多个属性。 可以传递具有相应属性的纯文本对象,也可以传递另一个相同类型的 API 对象。

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

参数

properties
Excel.Interfaces.PivotTableUpdateData

一个 JavaScript 对象,其属性与调用该方法的对象的属性同构结构。

options
OfficeExtension.UpdateOptions

提供一个选项,用于在 properties 对象尝试设置任何只读属性时禁止错误。

返回

void

set(properties)

基于现有加载的对象,同时在对象上设置多个属性。

set(properties: Excel.PivotTable): void;

参数

properties
Excel.PivotTable

返回

void

toJSON()

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

toJSON(): Excel.Interfaces.PivotTableData;

返回