Excel.PivotLayout class

Represents the visual layout of the PivotTable.

[ API set: ExcelApi 1.8 ]

Extends
OfficeExtension.ClientObject

Properties

autoFormat

Specifies whether formatting will be automatically formatted when it’s refreshed or when fields are moved

[ API set: ExcelApi 1.9 ]

context

The request context associated with the object. This connects the add-in's process to the Office host application's process.

enableFieldList

Specifies whether the field list can be shown in the UI.

[ API set: ExcelApi BETA (PREVIEW ONLY) ]

layoutType

This property indicates the PivotLayoutType of all fields on the PivotTable. If fields have different states, this will be null.

[ API set: ExcelApi 1.8 ]

preserveFormatting

Specifies whether formatting is preserved when the report is refreshed or recalculated by operations such as pivoting, sorting, or changing page field items.

[ API set: ExcelApi 1.9 ]

showColumnGrandTotals

Specifies whether the PivotTable report shows grand totals for columns.

[ API set: ExcelApi 1.8 ]

showRowGrandTotals

Specifies whether the PivotTable report shows grand totals for rows.

[ API set: ExcelApi 1.8 ]

subtotalLocation

This property indicates the SubtotalLocationType of all fields on the PivotTable. If fields have different states, this will be null.

[ API set: ExcelApi 1.8 ]

Methods

getCell(dataHierarchy, rowItems, columnItems)

Gets a unique cell in the PivotTable based on a data hierarchy and the row and column items of their respective hierarchies. The returned cell is the intersection of the given row and column that contains the data from the given hierarchy. This method is the inverse of calling getPivotItems and getDataHierarchy on a particular cell.

[ API set: ExcelApi BETA (PREVIEW ONLY) ]

getColumnLabelRange()

Returns the range where the PivotTable's column labels reside.

[ API set: ExcelApi 1.8 ]

getDataBodyRange()

Returns the range where the PivotTable's data values reside.

[ API set: ExcelApi 1.8 ]

getDataHierarchy(cell)

Gets the DataHierarchy that is used to calculate the value in a specified range within the PivotTable.

[ API set: ExcelApi 1.9 ]

getFilterAxisRange()

Returns the range of the PivotTable's filter area.

[ API set: ExcelApi 1.8 ]

getPivotItems(axis, cell)

Gets the PivotItems from an axis that make up the value in a specified range within the PivotTable.

[ API set: ExcelApi 1.9 ]

getPivotItems(axisString, cell)

Gets the PivotItems from an axis that make up the value in a specified range within the PivotTable.

[ API set: ExcelApi 1.9 ]

getRange()

Returns the range the PivotTable exists on, excluding the filter area.

[ API set: ExcelApi 1.8 ]

getRowLabelRange()

Returns the range where the PivotTable's row labels reside.

[ API set: ExcelApi 1.8 ]

load(option)

Queues up a command to load the specified properties of the object. You must call "context.sync()" before reading the properties.

load(propertyNames)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

load(propertyNamesAndPaths)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

set(properties, options)

Sets multiple properties of an object at the same time. 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.

setAutoSortOnCell(cell, sortBy)

Sets the PivotTable to automatically sort using the specified cell to automatically select all necessary criteria and context. This behaves identically to applying an autosort from the UI.

[ API set: ExcelApi 1.9 ]

setAutoSortOnCell(cell, sortByString)

Sets the PivotTable to automatically sort using the specified cell to automatically select all necessary criteria and context. This behaves identically to applying an autosort from the UI.

[ API set: ExcelApi 1.9 ]

toJSON()

Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify, in turn, calls the toJSON method of the object that is passed to it.) Whereas the original Excel.PivotLayout object is an API object, the toJSON method returns a plain JavaScript object (typed as Excel.Interfaces.PivotLayoutData) that contains shallow copies of any loaded child properties from the original object.

Property Details

autoFormat

Specifies whether formatting will be automatically formatted when it’s refreshed or when fields are moved

[ API set: ExcelApi 1.9 ]

autoFormat: boolean;

Property Value

boolean

context

The request context associated with the object. This connects the add-in's process to the Office host application's process.

context: RequestContext;

Property Value

enableFieldList

Note

This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.

Specifies whether the field list can be shown in the UI.

[ API set: ExcelApi BETA (PREVIEW ONLY) ]

enableFieldList: boolean;

Property Value

boolean

layoutType

This property indicates the PivotLayoutType of all fields on the PivotTable. If fields have different states, this will be null.

[ API set: ExcelApi 1.8 ]

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

Property Value

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

preserveFormatting

Specifies whether formatting is preserved when the report is refreshed or recalculated by operations such as pivoting, sorting, or changing page field items.

[ API set: ExcelApi 1.9 ]

preserveFormatting: boolean;

Property Value

boolean

showColumnGrandTotals

Specifies whether the PivotTable report shows grand totals for columns.

[ API set: ExcelApi 1.8 ]

showColumnGrandTotals: boolean;

Property Value

boolean

showRowGrandTotals

Specifies whether the PivotTable report shows grand totals for rows.

[ API set: ExcelApi 1.8 ]

showRowGrandTotals: boolean;

Property Value

boolean

subtotalLocation

This property indicates the SubtotalLocationType of all fields on the PivotTable. If fields have different states, this will be null.

[ API set: ExcelApi 1.8 ]

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

Property Value

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

Method Details

getCell(dataHierarchy, rowItems, columnItems)

Note

This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.

Gets a unique cell in the PivotTable based on a data hierarchy and the row and column items of their respective hierarchies. The returned cell is the intersection of the given row and column that contains the data from the given hierarchy. This method is the inverse of calling getPivotItems and getDataHierarchy on a particular cell.

[ API set: ExcelApi BETA (PREVIEW ONLY) ]

getCell(dataHierarchy: DataPivotHierarchy | string, rowItems: Array<PivotItem | string>, columnItems: Array<PivotItem | string>): Excel.Range;

Parameters

dataHierarchy
DataPivotHierarchy | string

The dataHierarchy that provides the data item to find.

rowItems
Array<PivotItem | string>

The PivotItems from the row axis that make up the value to find.

columnItems
Array<PivotItem | string>

The PivotItems from the column axis that make up the value to find.

Returns

A range specifying a single cell that contains the value specified.

getColumnLabelRange()

Returns the range where the PivotTable's column labels reside.

[ API set: ExcelApi 1.8 ]

getColumnLabelRange(): Excel.Range;

Returns

getDataBodyRange()

Returns the range where the PivotTable's data values reside.

[ API set: ExcelApi 1.8 ]

getDataBodyRange(): Excel.Range;

Returns

Examples

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)

Gets the DataHierarchy that is used to calculate the value in a specified range within the PivotTable.

[ API set: ExcelApi 1.9 ]

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

Parameters

cell
Range | string

A single cell within the PivotTable data body.

Returns

The DataPivotHierarchy object used to calculate the value in the specified cell.

getFilterAxisRange()

Returns the range of the PivotTable's filter area.

[ API set: ExcelApi 1.8 ]

getFilterAxisRange(): Excel.Range;

Returns

getPivotItems(axis, cell)

Gets the PivotItems from an axis that make up the value in a specified range within the PivotTable.

[ API set: ExcelApi 1.9 ]

getPivotItems(axis: Excel.PivotAxis, cell: Range | string): OfficeExtension.ClientResult<Excel.PivotItem[]>;

Parameters

axis
Excel.PivotAxis

The axis from which to get the PivotItems. Must be either "row" or "column."

cell
Range | string

A single cell within the PivotTable's data body.

Returns

OfficeExtension.ClientResult<Excel.PivotItem[]>

A collection of PivotItems that are used to calculate the values in the specified row.

getPivotItems(axisString, cell)

Gets the PivotItems from an axis that make up the value in a specified range within the PivotTable.

[ API set: ExcelApi 1.9 ]

getPivotItems(axisString: "Unknown" | "Row" | "Column" | "Data" | "Filter", cell: Range | string): OfficeExtension.ClientResult<Excel.PivotItem[]>;

Parameters

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

The axis from which to get the PivotItems. Must be either "row" or "column."

cell
Range | string

A single cell within the PivotTable's data body.

Returns

OfficeExtension.ClientResult<Excel.PivotItem[]>

A collection of PivotItems that are used to calculate the values in the specified row.

getRange()

Returns the range the PivotTable exists on, excluding the filter area.

[ API set: ExcelApi 1.8 ]

getRange(): Excel.Range;

Returns

getRowLabelRange()

Returns the range where the PivotTable's row labels reside.

[ API set: ExcelApi 1.8 ]

getRowLabelRange(): Excel.Range;

Returns

load(option)

Queues up a command to load the specified properties of the object. You must call "context.sync()" before reading the properties.

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

Parameters

Returns

Remarks

In addition to this signature, this method has the following signatures:

load(option?: string | string[]): Excel.PivotLayout - 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.PivotLayout - 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.PivotLayout - Only available on collection types. It is similar to the preceding signature. Option.top specifies the maximum number of collection items that can be included in the result. Option.skip specifies the number of items that are to be skipped and not included in the result. 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. You must call context.sync() before reading the properties.

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

Parameters

propertyNames
string | string[]

A comma-delimited string or an array of strings that specify the properties to load.

Returns

load(propertyNamesAndPaths)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

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

Parameters

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

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.

Returns

set(properties, options)

Sets multiple properties of an object at the same time. You can pass either a plain object with the appropriate properties, or another API object of the same type.

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

Parameters

properties
Interfaces.PivotLayoutUpdateData

A JavaScript object with properties that are structured isomorphically to the properties of the object on which the method is called.

options
OfficeExtension.UpdateOptions

Provides an option to suppress errors if the properties object tries to set any read-only properties.

Returns

void

Remarks

This method has the following additional signature:

set(properties: Excel.PivotLayout): void

set(properties)

Sets multiple properties on the object at the same time, based on an existing loaded object.

set(properties: Excel.PivotLayout): void;

Parameters

properties
Excel.PivotLayout

Returns

void

setAutoSortOnCell(cell, sortBy)

Sets the PivotTable to automatically sort using the specified cell to automatically select all necessary criteria and context. This behaves identically to applying an autosort from the UI.

[ API set: ExcelApi 1.9 ]

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

Parameters

cell
Range | string

A single cell to use get the criteria from for applying the autosort.

sortBy
Excel.SortBy

The direction of the sort.

Returns

void

setAutoSortOnCell(cell, sortByString)

Sets the PivotTable to automatically sort using the specified cell to automatically select all necessary criteria and context. This behaves identically to applying an autosort from the UI.

[ API set: ExcelApi 1.9 ]

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

Parameters

cell
Range | string

A single cell to use get the criteria from for applying the autosort.

sortByString
"Ascending" | "Descending"

The direction of the sort.

Returns

void

toJSON()

Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify, in turn, calls the toJSON method of the object that is passed to it.) Whereas the original Excel.PivotLayout object is an API object, the toJSON method returns a plain JavaScript object (typed as Excel.Interfaces.PivotLayoutData) that contains shallow copies of any loaded child properties from the original object.

toJSON(): Excel.Interfaces.PivotLayoutData;

Returns