ExcelScript.PivotFilters interface
An interface representing all PivotFilters currently applied to a given PivotField.
Properties
date |
The PivotField's currently applied date filter. This property is |
label |
The PivotField's currently applied label filter. This property is |
manual |
The PivotField's currently applied manual filter. This property is |
value |
The PivotField's currently applied value filter. This property is |
Property Details
dateFilter
The PivotField's currently applied date filter. This property is null
if no value filter is applied.
dateFilter?: PivotDateFilter;
Property Value
Examples
/**
* 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
The PivotField's currently applied label filter. This property is null
if no value filter is applied.
labelFilter?: PivotLabelFilter;
Property Value
Examples
/**
* 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
The PivotField's currently applied manual filter. This property is null
if no value filter is applied.
manualFilter?: PivotManualFilter;
Property Value
Examples
/**
* 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
The PivotField's currently applied value filter. This property is null
if no value filter is applied.
valueFilter?: PivotValueFilter;
Property Value
Examples
/**
* 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
});
}
Office Scripts