ExcelScript.PivotField interface

表示 Excel PivotField。

方法

applyFilter(filter)

设置字段的一个或多个当前数据透视筛选器,并将其应用于字段。 如果提供的筛选器无效或无法应用,则会引发异常。

clearAllFilters()

清除所有字段筛选器中的所有条件。 这会删除字段上的任何活动筛选。

clearFilter(filterType)

清除给定类型的字段筛选器中的所有现有条件, (当前) 应用条件。

getFilters()

获取字段上当前应用的所有筛选器。

getId()

PivotField 的 ID。

getItems()

返回与 PivotField 关联的 PivotItems。

getName()

PivotField 的名称。

getPivotItem(name)

按名称获取 PivotItem。 如果 PivotItem 不存在,则此方法返回 undefined

getShowAllItems()

确定是否显示 PivotField 的所有项。

getSubtotals()

PivotField 小计。

isFiltered(filterType)

检查字段上是否有任何已应用的筛选器。

setName(name)

PivotField 的名称。

setShowAllItems(showAllItems)

确定是否显示 PivotField 的所有项。

setSubtotals(subtotals)

PivotField 小计。

sortByLabels(sortBy)

PivotField 排序。 如果指定 DataPivotHierarchy,则会基于它进行排序,如果未指定,则会基于 PivotField 本身进行排序。

sortByValues(sortBy, valuesHierarchy, pivotItemScope)

按给定范围中的指定值对 PivotField 进行排序。 范围定义当同一 DataPivotHierarchy 中存在多个值时,将使用哪些特定值进行排序。

方法详细信息

applyFilter(filter)

设置字段的一个或多个当前数据透视筛选器,并将其应用于字段。 如果提供的筛选器无效或无法应用,则会引发异常。

applyFilter(filter: PivotFilters): void;

参数

filter
ExcelScript.PivotFilters

配置的特定 PivotFilter,或包含多个已配置筛选器的 PivotFilters 接口。

返回

void

示例

/**
 * This script applies a PivotValueFilter to the first row hierarchy in the PivotTable.
 */
function main(workbook: ExcelScript.Workbook) {
  // Get the PivotTable on the current worksheet.
  let sheet = workbook.getActiveWorksheet();
  let pivotTable = sheet.getPivotTables()[0];

  // Get the first row hierarchy to use as the field which gets filtered.
  let rowHierarchy = pivotTable.getRowHierarchies()[0];

  // Get the first data hierarchy to use as the values for filtering the rows.
  let dataHierarchy = pivotTable.getDataHierarchies()[0];

  // Create a filter that excludes values greater than 500.
  let filter: ExcelScript.PivotValueFilter = {
    condition: ExcelScript.ValueFilterCondition.greaterThan,
    comparator: 500,
    value: dataHierarchy.getName()
  };

  // Apply the filter.
  rowHierarchy.getPivotField(rowHierarchy.getName()).applyFilter({
    valueFilter: filter
  });
}

clearAllFilters()

清除所有字段筛选器中的所有条件。 这会删除字段上的任何活动筛选。

clearAllFilters(): void;

返回

void

clearFilter(filterType)

清除给定类型的字段筛选器中的所有现有条件, (当前) 应用条件。

clearFilter(filterType: PivotFilterType): void;

参数

filterType
ExcelScript.PivotFilterType

要清除其所有条件的字段的筛选器类型。

返回

void

示例

/**
 * This script gets the "Type" field from the "Farms Sales" PivotTable 
 * and clears the value filter from it.
 */ 
function main(workbook: ExcelScript.Workbook) {
  // Get the PivotTable named "Farm Sales".
  const pivot = workbook.getPivotTable("Farm Sales");

  // Get the "Type" field.
  const typeField = pivot.getHierarchy("Type").getPivotField("Type");

  // Clear the value filter (if there is one) from the field.
  typeField.clearFilter(ExcelScript.PivotFilterType.value);
}

getFilters()

获取字段上当前应用的所有筛选器。

getFilters(): PivotFilters;

返回

getId()

PivotField 的 ID。

getId(): string;

返回

string

getItems()

返回与 PivotField 关联的 PivotItems。

getItems(): PivotItem[];

返回

getName()

PivotField 的名称。

getName(): string;

返回

string

getPivotItem(name)

按名称获取 PivotItem。 如果 PivotItem 不存在,则此方法返回 undefined

getPivotItem(name: string): PivotItem | undefined;

参数

name

string

要检索的 PivotItem 的名称。

返回

ExcelScript.PivotItem | undefined

getShowAllItems()

确定是否显示 PivotField 的所有项。

getShowAllItems(): boolean;

返回

boolean

getSubtotals()

PivotField 小计。

getSubtotals(): Subtotals;

返回

isFiltered(filterType)

检查字段上是否有任何已应用的筛选器。

isFiltered(filterType?: PivotFilterType): boolean;

参数

filterType
ExcelScript.PivotFilterType

要检查的筛选器类型。 如果未提供任何类型,则此方法将在应用任何筛选器时检查。

返回

boolean

setName(name)

PivotField 的名称。

setName(name: string): void;

参数

name

string

返回

void

setShowAllItems(showAllItems)

确定是否显示 PivotField 的所有项。

setShowAllItems(showAllItems: boolean): void;

参数

showAllItems

boolean

返回

void

setSubtotals(subtotals)

PivotField 小计。

setSubtotals(subtotals: Subtotals): void;

参数

返回

void

sortByLabels(sortBy)

PivotField 排序。 如果指定 DataPivotHierarchy,则会基于它进行排序,如果未指定,则会基于 PivotField 本身进行排序。

sortByLabels(sortBy: SortBy): void;

参数

sortBy
ExcelScript.SortBy

指定排序是按升序还是降序进行。

返回

void

sortByValues(sortBy, valuesHierarchy, pivotItemScope)

按给定范围中的指定值对 PivotField 进行排序。 范围定义当同一 DataPivotHierarchy 中存在多个值时,将使用哪些特定值进行排序。

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

参数

sortBy
ExcelScript.SortBy

指定排序是按升序还是降序进行。

valuesHierarchy
ExcelScript.DataPivotHierarchy

指定要用于排序的数据轴上的值层次结构。

pivotItemScope

Array<ExcelScript.PivotItem | string>

应用于排序范围的项。 这些将是构成要排序的行或列的项。 如果使用字符串而不是 PivotItem,则字符串表示 PivotItem 的 ID。 如果要排序的轴上除了数据层次结构之外,没有其他项,则此项可以为空。

返回

void

示例

/**
 * This sample sorts the rows of a PivotTable.
 */
function main(workbook: ExcelScript.Workbook) {
  // Get an existing PivotTable.
  const pivotTable = workbook.getPivotTable("Farm Sales");

  // Get the data hierarchy to use as the basis of the sort.
  const valueFieldToSortOn = pivotTable.getDataHierarchy("Sum of Crates Sold Wholesale");

  // Get the row to sort.
  const rowToSort = pivotTable.getRowHierarchy("Farm");

  // Sort the "Farm" row's only field by the values in "Sum of Crates Sold Wholesale".
  rowToSort.getFields()[0].sortByValues(ExcelScript.SortBy.descending, valueFieldToSortOn);
}