Excel.PivotLayout class

表示数据透视表的视觉布局。

扩展

属性

autoFormat

指定刷新或移动字段时是否自动设置格式设置格式。

context

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

layoutType

此属性指示数据透视表上的所有字段的 PivotLayoutType。 如果字段状态不同,则为 null。

preserveFormatting

指定通过透视、排序或更改页字段项等操作刷新或重新计算报表时是否保留格式。

showColumnGrandTotals

指定数据透视表是否显示列的总计。

showRowGrandTotals

指定数据透视表是否显示行的总计。

subtotalLocation

此属性指示数据透视表上所有字段的 。SubtotalLocationType 如果字段具有不同的状态,这将是 null

方法

getColumnLabelRange()

返回数据透视表列标签所在位置的区域。

getDataBodyRange()

返回数据透视表数据值所在位置的区域。

getDataHierarchy(cell)

获取 DataHierarchy,它用于计算数据透视表中指定区域内的值。

getFilterAxisRange()

返回数据透视表筛选区的区域。

getPivotItems(axis, cell)

从构成数据透视表中指定区域内的值的轴获取 PivotItems。

getPivotItems(axis, cell)

从构成数据透视表中指定区域内的值的轴获取 PivotItems。

getRange()

返回存在数据透视表的区域,不包括筛选区。

getRowLabelRange()

返回数据透视表行标签所在位置的区域。

load(options)

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

load(propertyNames)

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

load(propertyNamesAndPaths)

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

set(properties, options)

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

set(properties)

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

setAutoSortOnCell(cell, sortBy)

将数据透视表设置为使用指定的单元格设置自动排序,以自动选择排序的所有条件和上下文。 这与从 UI 应用自动排序的行为相同。

setAutoSortOnCell(cell, sortBy)

将数据透视表设置为使用指定的单元格设置自动排序,以自动选择排序的所有条件和上下文。 这与从 UI 应用自动排序的行为相同。

toJSON()

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

属性详细信息

autoFormat

指定刷新或移动字段时是否自动设置格式设置格式。

autoFormat: boolean;

属性值

boolean

注解

API 集:ExcelApi 1.9

context

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

context: RequestContext;

属性值

layoutType

此属性指示数据透视表上的所有字段的 PivotLayoutType。 如果字段状态不同,则为 null。

layoutType: Excel.PivotLayoutType | "Compact" | "Tabular" | "Outline";

属性值

Excel.PivotLayoutType | "Compact" | "Tabular" | "Outline"

注解

API 集:ExcelApi 1.8

示例

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

await Excel.run(async (context) => {
  // Change the PivotLayout.type to a new type.
  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);
});

preserveFormatting

指定通过透视、排序或更改页字段项等操作刷新或重新计算报表时是否保留格式。

preserveFormatting: boolean;

属性值

boolean

注解

API 集:ExcelApi 1.9

示例

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

await Excel.run(async (context) => {
  // Set whether the PivotTable keeps the established format after it is refreshed and recalculated.
  const pivotTable = context.workbook.pivotTables.getItem("Farm Sales");
  const pivotLayout = pivotTable.layout;

  pivotLayout.load("preserveFormatting");
  await context.sync();

  let preserveFormattingToSet = !pivotLayout.preserveFormatting;
  console.log(`Preserve the formatting PivotTable after a refresh? - ${preserveFormattingToSet}`);

  pivotLayout.preserveFormatting = preserveFormattingToSet;
  await context.sync();
});

showColumnGrandTotals

指定数据透视表是否显示列的总计。

showColumnGrandTotals: boolean;

属性值

boolean

注解

API 集:ExcelApi 1.8

示例

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

await Excel.run(async (context) => {
  // Turn the grand totals on and off for the rows and columns.
  const pivotTable = context.workbook.pivotTables.getItem("Farm Sales");
  const pivotLayout = pivotTable.layout;

  pivotLayout.load(["showRowGrandTotals", "showColumnGrandTotals"]);
  await context.sync();

  let showColumnTotals = !pivotLayout.showColumnGrandTotals;
  let showRowTotals = !pivotLayout.showRowGrandTotals;
  console.log(`Show column grand totals? - ${showColumnTotals}`);
  console.log(`Show row grand totals? - ${showRowTotals}`);

  pivotLayout.showColumnGrandTotals = showColumnTotals;
  pivotLayout.showRowGrandTotals = showRowTotals;

  await context.sync();
});

showRowGrandTotals

指定数据透视表是否显示行的总计。

showRowGrandTotals: boolean;

属性值

boolean

注解

API 集:ExcelApi 1.8

示例

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

await Excel.run(async (context) => {
  // Turn the grand totals on and off for the rows and columns.
  const pivotTable = context.workbook.pivotTables.getItem("Farm Sales");
  const pivotLayout = pivotTable.layout;

  pivotLayout.load(["showRowGrandTotals", "showColumnGrandTotals"]);
  await context.sync();

  let showColumnTotals = !pivotLayout.showColumnGrandTotals;
  let showRowTotals = !pivotLayout.showRowGrandTotals;
  console.log(`Show column grand totals? - ${showColumnTotals}`);
  console.log(`Show row grand totals? - ${showRowTotals}`);

  pivotLayout.showColumnGrandTotals = showColumnTotals;
  pivotLayout.showRowGrandTotals = showRowTotals;

  await context.sync();
});

subtotalLocation

此属性指示数据透视表上所有字段的 。SubtotalLocationType 如果字段具有不同的状态,这将是 null

subtotalLocation: Excel.SubtotalLocationType | "AtTop" | "AtBottom" | "Off";

属性值

Excel.SubtotalLocationType | "AtTop" | "AtBottom" | "Off"

注解

API 集:ExcelApi 1.8

方法详细信息

getColumnLabelRange()

返回数据透视表列标签所在位置的区域。

getColumnLabelRange(): Excel.Range;

返回

注解

API 集:ExcelApi 1.8

getDataBodyRange()

返回数据透视表数据值所在位置的区域。

getDataBodyRange(): Excel.Range;

返回

注解

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

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

    // The layout controls the ranges used by the PivotTable.
    const range = pivotTable.layout.getDataBodyRange();

    // Get all the data hierarchy totals.
    const grandTotalRange = range.getLastRow();
    grandTotalRange.load("address");
    await context.sync();
    
    // Use the wholesale and farm sale totals to make a final sum.
    const masterTotalRange = context.workbook.worksheets.getActiveWorksheet().getRange("B27:C27");
    masterTotalRange.formulas = [["All Crates", "=SUM(" + grandTotalRange.address + ")"]];
    await context.sync();
});

getDataHierarchy(cell)

获取 DataHierarchy,它用于计算数据透视表中指定区域内的值。

getDataHierarchy(cell: Range | string): Excel.DataPivotHierarchy;

参数

cell

Excel.Range | string

数据透视表数据正文中的单个单元格。

返回

用于计算指定单元格中的值的 DataPivotHierarchy 对象。

注解

API 集:ExcelApi 1.9

getFilterAxisRange()

返回数据透视表筛选区的区域。

getFilterAxisRange(): Excel.Range;

返回

注解

API 集:ExcelApi 1.8

getPivotItems(axis, cell)

从构成数据透视表中指定区域内的值的轴获取 PivotItems。

getPivotItems(axis: Excel.PivotAxis, cell: Range | string): Excel.PivotItemCollection;

参数

axis
Excel.PivotAxis

从中获取 PivotItems 的轴。 必须为“行”或“列”。

cell

Excel.Range | string

数据透视表数据正文中的单个单元格。

返回

用于计算指定行中值的数据透视项的 PivotItemCollection。

注解

API 集:ExcelApi 1.9

getPivotItems(axis, cell)

从构成数据透视表中指定区域内的值的轴获取 PivotItems。

getPivotItems(axis: "Unknown" | "Row" | "Column" | "Data" | "Filter", cell: Range | string): Excel.PivotItemCollection;

参数

axis

"Unknown" | "Row" | "Column" | "Data" | "Filter"

从中获取 PivotItems 的轴。 必须为“行”或“列”。

cell

Excel.Range | string

数据透视表数据正文中的单个单元格。

返回

用于计算指定行中值的数据透视项的 PivotItemCollection。

注解

API 集:ExcelApi 1.9

getRange()

返回存在数据透视表的区域,不包括筛选区。

getRange(): Excel.Range;

返回

注解

API 集:ExcelApi 1.8

getRowLabelRange()

返回数据透视表行标签所在位置的区域。

getRowLabelRange(): Excel.Range;

返回

注解

API 集:ExcelApi 1.8

load(options)

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

load(options?: Excel.Interfaces.PivotLayoutLoadOptions): Excel.PivotLayout;

参数

options
Excel.Interfaces.PivotLayoutLoadOptions

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

返回

load(propertyNames)

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

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

参数

propertyNames

string | string[]

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

返回

load(propertyNamesAndPaths)

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

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

参数

propertyNamesAndPaths

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

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

返回

set(properties, options)

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

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

参数

properties
Excel.Interfaces.PivotLayoutUpdateData

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

options
OfficeExtension.UpdateOptions

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

返回

void

set(properties)

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

set(properties: Excel.PivotLayout): void;

参数

properties
Excel.PivotLayout

返回

void

setAutoSortOnCell(cell, sortBy)

将数据透视表设置为使用指定的单元格设置自动排序,以自动选择排序的所有条件和上下文。 这与从 UI 应用自动排序的行为相同。

setAutoSortOnCell(cell: Range | string, sortBy: Excel.SortBy): void;

参数

cell

Excel.Range | string

要使用的单个单元格获取应用自动排序的条件。

sortBy
Excel.SortBy

排序的方向。

返回

void

注解

API 集:ExcelApi 1.9

setAutoSortOnCell(cell, sortBy)

将数据透视表设置为使用指定的单元格设置自动排序,以自动选择排序的所有条件和上下文。 这与从 UI 应用自动排序的行为相同。

setAutoSortOnCell(cell: Range | string, sortBy: "Ascending" | "Descending"): void;

参数

cell

Excel.Range | string

要使用的单个单元格获取应用自动排序的条件。

sortBy

"Ascending" | "Descending"

排序的方向。

返回

void

注解

API 集:ExcelApi 1.9

toJSON()

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

toJSON(): Excel.Interfaces.PivotLayoutData;

返回