Excel.PivotField class

Represents the Excel PivotField.

Extends

Remarks

[ API set: ExcelApi 1.8 ]

Properties

context

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

id

ID of the PivotField.

items

Returns the PivotItems associated with the PivotField.

name

Name of the PivotField.

showAllItems

Determines whether to show all items of the PivotField.

subtotals

Subtotals of the PivotField.

Methods

applyFilter(filter)

Sets one or more of the field's current PivotFilters and applies them to the field. If the provided filters are invalid or cannot be applied, an exception is thrown.

clearAllFilters()

Clears all criteria from all of the field's filters. This removes any active filtering on the field.

clearFilter(filterType)

Clears all existing criteria from the field's filter of the given type (if one is currently applied).

clearFilter(filterTypeString)

Clears all existing criteria from the field's filter of the given type (if one is currently applied).

getFilters()

Gets all filters currently applied on the field.

isFiltered(filterType)

Checks if there are any applied filters on the field.

isFiltered(filterTypeString)

Checks if there are any applied filters on the field.

load(options)

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.

sortByLabels(sortBy)

Sorts the PivotField. If a DataPivotHierarchy is specified, then sort will be applied based on it, if not sort will be based on the PivotField itself.

sortByValues(sortBy, valuesHierarchy, pivotItemScope)

Sorts the PivotField by specified values in a given scope. The scope defines which specific values will be used to sort when there are multiple values from the same DataPivotHierarchy.

sortByValues(sortByString, valuesHierarchy, pivotItemScope)

Sorts the PivotField by specified values in a given scope. The scope defines which specific values will be used to sort when there are multiple values from the same DataPivotHierarchy.

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.PivotField object is an API object, the toJSON method returns a plain JavaScript object (typed as Excel.Interfaces.PivotFieldData) that contains shallow copies of any loaded child properties from the original object.

Property Details

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

id

ID of the PivotField.

readonly id: string;

Property Value

string

Remarks

[ API set: ExcelApi 1.8 ]

items

Returns the PivotItems associated with the PivotField.

readonly items: Excel.PivotItemCollection;

Property Value

Remarks

[ API set: ExcelApi 1.8 ]

name

Name of the PivotField.

name: string;

Property Value

string

Remarks

[ API set: ExcelApi 1.8 ]

showAllItems

Determines whether to show all items of the PivotField.

showAllItems: boolean;

Property Value

boolean

Remarks

[ API set: ExcelApi 1.8 ]

subtotals

Subtotals of the PivotField.

subtotals: Excel.Subtotals;

Property Value

Remarks

[ API set: ExcelApi 1.8 ]

Method Details

applyFilter(filter)

Sets one or more of the field's current PivotFilters and applies them to the field. If the provided filters are invalid or cannot be applied, an exception is thrown.

applyFilter(filter: Excel.PivotFilters): void;

Parameters

filter
Excel.PivotFilters

A configured specific PivotFilter, or a PivotFilters interface containing multiple configured filters.

Returns

void

Remarks

[ API set: ExcelApi 1.12 ]

clearAllFilters()

Clears all criteria from all of the field's filters. This removes any active filtering on the field.

clearAllFilters(): void;

Returns

void

Remarks

[ API set: ExcelApi 1.12 ]

Examples

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

await Excel.run(async (context) => {
  // Clear all the PivotFilters.

  // Get the PivotTable.
  const pivotTable = context.workbook.worksheets.getActiveWorksheet().pivotTables.getItem("Farm Sales");
  pivotTable.hierarchies.load("name");
  await context.sync();

  // Clear the filters on each PivotField.
  pivotTable.hierarchies.items.forEach((hierarchy) => {
    hierarchy.fields.getItem(hierarchy.name).clearAllFilters();
  });
  await context.sync();
});

clearFilter(filterType)

Clears all existing criteria from the field's filter of the given type (if one is currently applied).

clearFilter(filterType: Excel.PivotFilterType): void;

Parameters

filterType
Excel.PivotFilterType

The type of filter on the field of which to clear all criteria.

Returns

void

Remarks

[ API set: ExcelApi 1.12 ]

clearFilter(filterTypeString)

Clears all existing criteria from the field's filter of the given type (if one is currently applied).

clearFilter(filterTypeString: "Unknown" | "Value" | "Manual" | "Label" | "Date"): void;

Parameters

filterTypeString

"Unknown" | "Value" | "Manual" | "Label" | "Date"

The type of filter on the field of which to clear all criteria.

Returns

void

Remarks

[ API set: ExcelApi 1.12 ]

getFilters()

Gets all filters currently applied on the field.

getFilters(): OfficeExtension.ClientResult<Excel.PivotFilters>;

Returns

A PivotFilters interface with all active filters.

Remarks

[ API set: ExcelApi 1.12 ]

isFiltered(filterType)

Checks if there are any applied filters on the field.

isFiltered(filterType?: Excel.PivotFilterType): OfficeExtension.ClientResult<boolean>;

Parameters

filterType
Excel.PivotFilterType

The filter type to check. If no type is provided, this method will check if any filter is applied.

Returns

True if the field has a filter of type filterType applied. If filterType is not specified, true is returned if the field has any applied filters.

Remarks

[ API set: ExcelApi 1.12 ]

isFiltered(filterTypeString)

Checks if there are any applied filters on the field.

isFiltered(filterTypeString?: "Unknown" | "Value" | "Manual" | "Label" | "Date"): OfficeExtension.ClientResult<boolean>;

Parameters

filterTypeString

"Unknown" | "Value" | "Manual" | "Label" | "Date"

The filter type to check. If no type is provided, this method will check if any filter is applied.

Returns

True if the field has a filter of type filterType applied. If filterType is not specified, true is returned if the field has any applied filters.

Remarks

[ API set: ExcelApi 1.12 ]

load(options)

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

load(options?: Excel.Interfaces.PivotFieldLoadOptions): Excel.PivotField;

Parameters

options
Excel.Interfaces.PivotFieldLoadOptions

Provides options for which properties of the object to load.

Returns

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.PivotField;

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.PivotField;

Parameters

propertyNamesAndPaths

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

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.PivotFieldUpdateData, options?: OfficeExtension.UpdateOptions): void;

Parameters

properties
Excel.Interfaces.PivotFieldUpdateData

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

set(properties)

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

set(properties: Excel.PivotField): void;

Parameters

properties
Excel.PivotField

Returns

void

sortByLabels(sortBy)

Sorts the PivotField. If a DataPivotHierarchy is specified, then sort will be applied based on it, if not sort will be based on the PivotField itself.

sortByLabels(sortBy: SortBy): void;

Parameters

sortBy
Excel.SortBy

Specifies if the sorting is done in ascending or descending order.

Returns

void

Remarks

[ API set: ExcelApi 1.8 ]

sortByValues(sortBy, valuesHierarchy, pivotItemScope)

Sorts the PivotField by specified values in a given scope. The scope defines which specific values will be used to sort when there are multiple values from the same DataPivotHierarchy.

sortByValues(sortBy: Excel.SortBy, valuesHierarchy: Excel.DataPivotHierarchy, pivotItemScope?: Array<PivotItem | string>): void;

Parameters

sortBy
Excel.SortBy

Specifies if the sorting is done in ascending or descending order.

valuesHierarchy
Excel.DataPivotHierarchy

Specifies the values hierarchy on the data axis to be used for sorting.

pivotItemScope

Array<Excel.PivotItem | string>

The items that should be used for the scope of the sorting. These will be the items that make up the row or column that you want to sort on. If a string is used instead of a PivotItem, the string represents the ID of the PivotItem. If there are no items other than data hierarchy on the axis you want to sort on, this can be empty.

Returns

void

Remarks

[ API set: ExcelApi 1.9 ]

sortByValues(sortByString, valuesHierarchy, pivotItemScope)

Sorts the PivotField by specified values in a given scope. The scope defines which specific values will be used to sort when there are multiple values from the same DataPivotHierarchy.

sortByValues(sortByString: "Ascending" | "Descending", valuesHierarchy: Excel.DataPivotHierarchy, pivotItemScope?: Array<PivotItem | string>): void;

Parameters

sortByString

"Ascending" | "Descending"

Specifies if the sorting is done in ascending or descending order.

valuesHierarchy
Excel.DataPivotHierarchy

Specifies the values hierarchy on the data axis to be used for sorting.

pivotItemScope

Array<Excel.PivotItem | string>

The items that should be used for the scope of the sorting. These will be the items that make up the row or column that you want to sort on. If a string is used instead of a PivotItem, the string represents the ID of the PivotItem. If there are no items other than data hierarchy on the axis you want to sort on, this can be empty.

Returns

void

Remarks

[ 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.PivotField object is an API object, the toJSON method returns a plain JavaScript object (typed as Excel.Interfaces.PivotFieldData) that contains shallow copies of any loaded child properties from the original object.

toJSON(): Excel.Interfaces.PivotFieldData;

Returns