ExcelScript.PivotFilters interface

表示当前应用于给定 PivotField 的所有 PivotFilters 的接口。

属性

dateFilter

PivotField 当前应用的日期筛选器。 如果未应用值筛选器,则此属性 null 为 。

labelFilter

PivotField 当前应用的标签筛选器。 如果未应用值筛选器,则此属性 null 为 。

manualFilter

PivotField 当前应用的手动筛选器。 如果未应用值筛选器,则此属性 null 为 。

valueFilter

PivotField 的当前应用值筛选器。 如果未应用值筛选器,则此属性 null 为 。

属性详细信息

dateFilter

PivotField 当前应用的日期筛选器。 如果未应用值筛选器,则此属性 null 为 。

dateFilter?: PivotDateFilter;

属性值

示例

/**
 * This script applies a filter to a PivotTable that filters out rows 
 * that aren't from this month.
 */
function main(workbook: ExcelScript.Workbook) {
  // Get the "Date Recorded" field to filter. 
  // The data in this field must be dates in order for the filter to work.
  const pivot = workbook.getPivotTables()[0];
  const rowHierarchy = pivot.getRowHierarchy("Date Recorded");
  const rowField = rowHierarchy.getFields()[0];

  // Apply the date filter.
  rowField.applyFilter({
    dateFilter: {
      // Setting the condition to `thisMonth` means items that are before or
      // after this month will not be displayed.
      condition: ExcelScript.DateFilterCondition.thisMonth
    }
  });
}

labelFilter

PivotField 当前应用的标签筛选器。 如果未应用值筛选器,则此属性 null 为 。

labelFilter?: PivotLabelFilter;

属性值

示例

/**
 * This script filters items that start with "L" from the "Type" field
 * of the "Farm Sales" PivotTable.
 */
function main(workbook: ExcelScript.Workbook) {
  // Get the PivotTable.
  const pivotTable = workbook.getActiveWorksheet().getPivotTable("Farm Sales");

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

  // Filter out any types that start with "L" (such as "Lemons" and "Limes").
  const filter: ExcelScript.PivotLabelFilter = {
    condition: ExcelScript.LabelFilterCondition.beginsWith,
    substring: "L",
    exclusive: true
  };

  // Apply the label filter to the field.
  field.applyFilter({ labelFilter: filter });
}

manualFilter

PivotField 当前应用的手动筛选器。 如果未应用值筛选器,则此属性 null 为 。

manualFilter?: PivotManualFilter;

属性值

示例

/**
 * This script adds a manual filter to a PivotTable. 
 */
function main(workbook: ExcelScript.Workbook)
{
  // Get the first PivotTable in the workbook.
  const pivot = workbook.getPivotTables()[0];

  // Get the hierarchy to use as the filter.
  const location = pivot.getHierarchy("Location");

  // Use "Location" as the FilterHierarchy.
  pivot.addFilterHierarchy(location);

  // Select items for the filter.
  // Note that hierarchies and fields have a 1:1 relationship in Excel,
  // so `getFields()[0]` always gets the correct field.
  location.getFields()[0].applyFilter({
    manualFilter: {
      selectedItems: ["Seattle", "Chicago"]
    }
  });
}

valueFilter

PivotField 的当前应用值筛选器。 如果未应用值筛选器,则此属性 null 为 。

valueFilter?: PivotValueFilter;

属性值

示例

/**
 * 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
  });
}