Microsoft 365 and Office | Development | Office JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I am using Excel Javascript API to build Excel add-in with Pivot table (PivotFilter Function), I can add the filter with the following code from document.
but when I use remove function it removing the filter which I added, after agian I add in filter It getting error(An Internal error has occoued)
after excel is closed.
This below code only I did:
async function add_pivot_filter() {
await Excel.run(async (context) => {
await context.sync();
let pivotTable = context.workbook.worksheets.getActiveWorksheet().pivotTables.getItem("Farm Sales");
// check if the PivotTable already has rows
await context.sync();
// addind one filter
let classHierarchy = pivotTable.filterHierarchies.getItemOrNullObject("Classification");
await context.sync();
if (classHierarchy.isNullObject) {
classHierarchy = pivotTable.filterHierarchies.add(pivotTable.hierarchies.getItem("Classification"));
await context.sync()
}
// again adding another one filter
let framHierarchy = pivotTable.filterHierarchies.getItemOrNullObject("Farm");
await context.sync();
if (framHierarchy.isNullObject) {
framHierarchy = pivotTable.filterHierarchies.add(pivotTable.hierarchies.getItem("Farm"));
await context.sync()
}
// removing one filter
let filter_remove = pivotTable.filterHierarchies.getItemOrNullObject("Classification");
await context.sync();
if (filter_remove.isNullObject == false) {
pivotTable.filterHierarchies.remove(filter_remove);
await context.sync();
}
// again adding one filter
let TypeHierarchy = pivotTable.filterHierarchies.getItemOrNullObject("Type");
await context.sync();
if (TypeHierarchy.isNullObject) {
TypeHierarchy = pivotTable.filterHierarchies.add(pivotTable.hierarchies.getItem("Type"));
await context.sync()
}
});
}
Note:
* I am adding one filter it adding without any error.
* Again adding another one filter it adding without any error.
* After I removing one filter it removing.
* After adding one filter again the excel sheet was closed.
The error was((An Internal error has occoued)). error status code 5001