in office script test if an item is in a pivottable
Private research annotation
Sign in to add a private research label or note.
- User
in office script test if an item is in a pivottable
- Assistant
To test if an item is in a PivotTable in an Office script, you can use the
PivotTable.getDataHierarchy()method to retrieve the data hierarchy of the PivotTable, and then check if the item you are looking for is in the hierarchy.Here is an example script that demonstrates how to test if an item is in a PivotTable:
jsfunction isInPivotTable(pivotTableName, itemName) { // Get the PivotTable object let pivotTable = context.workbook.worksheets.getItemOrNullObject("Sheet1").pivotTables.getItem(pivotTableName); // Get the data hierarchy of the PivotTable let dataHierarchy = pivotTable.getDataHierarchy(); // Check if the item is in the hierarchy for (let i = 0; i < dataHierarchy.length; i++) { let hierarchyItem = dataHierarchy[i]; if (hierarchyItem.name === itemName) { return true; } } return false; }In this example, the
isInPivotTable()function takes two parameters:pivotTableName(the name of the PivotTable to check) anditemName(the name of the item to look for in the PivotTable). The function retrieves the PivotTable object using thegetItem()method, and then calls thegetDataHierarchy()method to get the data hierarchy of the PivotTable. It then iterates over the hierarchy to find the item, and returnstrueif it is found, andfalseif it is not found.Note that this script assumes that the PivotTable and the item you are looking for are in the same worksheet ("Sheet1"). You will need to modify the script accordingly if the PivotTable is in a different worksheet.
- User
how to modify the retain items policy of a pivot table in office script
- Assistant
To modify the "Retain items deleted from the data source" policy of a PivotTable in Office Scripts, you can use the
PivotTable.refresh()method with thePivotRefreshTypeparameter set toPivotRefreshType.RetainDeletedItems.Here is an example script that demonstrates how to modify the "Retain items deleted from the data source" policy of a PivotTable:
jsasync function setRetainDeletedItemsPolicy(pivotTableName, retainDeletedItems) { // Get the PivotTable object let pivotTable = context.workbook.worksheets.getItemOrNullObject("Sheet1").pivotTables.getItem(pivotTableName); // Set the RetainDeletedItems policy if (retainDeletedItems) { await pivotTable.refreshAsync(PivotRefreshType.RetainDeletedItems); } else { await pivotTable.refreshAsync(PivotRefreshType.ClearAllFilters); } } - User
is there a method to do this synchronously
- Assistant
Yes, there is a synchronous version of the
refreshAsync()method calledrefresh(). You can use this method to modify the "Retain items deleted from the data source" policy of a PivotTable synchronously.Here is an example script that demonstrates how to modify the "Retain items deleted from the data source" policy of a PivotTable synchronously:
jsfunction setRetainDeletedItemsPolicy(pivotTableName, retainDeletedItems) { // Get the PivotTable object let pivotTable = context.workbook.worksheets.getItemOrNullObject("Sheet1").pivotTables.getItem(pivotTableName); // Set the RetainDeletedItems policy if (retainDeletedItems) { pivotTable.refresh(PivotRefreshType.RetainDeletedItems); } else { pivotTable.refresh(PivotRefreshType.ClearAllFilters); } } - User
the method "refresh" takes no argument
- Assistant
I apologize for the confusion. You are correct that the
refresh()method for PivotTables does not take any arguments.To modify the "Retain items deleted from the data source" policy of a PivotTable synchronously in Office Scripts, you can set the
retainDataproperty of thePivotTableobject totrueorfalsedepending on whether you want to retain deleted items or not, and then call therefresh()method.Here is an example script that demonstrates how to modify the "Retain items deleted from the data source" policy of a PivotTable synchronously:
js - User
pivotTable does not have a property "retainData"
- Assistant
I apologize for the confusion. You are correct that the
retainDataproperty is not available in Office Scripts.To modify the "Retain items deleted from the data source" policy of a PivotTable in Office Scripts, you can use the
PivotTable.refresh()method with thePivotRefreshTypeparameter set toPivotRefreshType.RetainDeletedItemsorPivotRefreshType.ClearAllFilters, depending on whether you want to retain deleted items or not.Here is an example script that demonstrates how to modify the "Retain items deleted from the data source" policy of a PivotTable:
jsasync function setRetainDeletedItemsPolicy(pivotTableName, retainDeletedItems) { // Get the PivotTable object let pivotTable = context.workbook.worksheets.getItemOrNullObject("Sheet1").pivotTables.getItem(pivotTableName); // Set the RetainDeletedItems policy if (retainDeletedItems) { await pivotTable.refreshAsync(PivotRefreshType.RetainDeletedItems); } else { await pivotTable.refreshAsync(PivotRefreshType.ClearAllFilters); } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.