OfficeScripts to read Pivot Table properties

Here’s a sample OfficeScript that demonstrates how to read various properties of a pivot table in Excel:

function readPivotTableProperties() { // Get the active worksheet var worksheet = context.workbook.getActiveWorksheet(); // Get a pivot table to read properties from var pivotTable = worksheet.getPivotTables().getAt(0); // Assuming the first pivot table in the worksheet // Read pivot table properties var pivotTableName = pivotTable.getName(); var pivotTableSourceRange = pivotTable.getSourceData(); var pivotTableColumnCount = pivotTable.getColumns().getCount(); var pivotTableRowCount = pivotTable.getRows().getCount(); var pivotTableValueCount = pivotTable.getValues().getCount(); // Logging the pivot table properties console.log("Pivot Table Name: " + pivotTableName); console.log("Pivot Table Source Range: " + pivotTableSourceRange); console.log("Pivot Table Column Count: " + pivotTableColumnCount); console.log("Pivot Table Row Count: " + pivotTableRowCount); console.log("Pivot Table Value Count: " + pivotTableValueCount); }

In this example, the readPivotTableProperties function performs the following steps:

  1. It gets the active worksheet using the getActiveWorksheet method.

  2. It retrieves the first pivot table in the worksheet using the getPivotTables().getAt(0) method. If you have multiple pivot tables, adjust the index as needed.

  3. It reads various properties of the pivot table, such as the name, source range, column count, row count, and value count.

  4. The pivot table properties are stored in variables: pivotTableName, pivotTableSourceRange, pivotTableColumnCount, pivotTableRowCount, and pivotTableValueCount, respectively.

  5. Finally, it logs the pivot table properties to the console.

You can call the readPivotTableProperties function to see the pivot table properties in the console output.

Please note that this example assumes that you have a workbook open and a worksheet with at least one pivot table in Excel for the web or Excel Online, where you can execute the OfficeScript.

Additionally, remember to set up the necessary context and load the OfficeScript runtime to ensure the script executes successfully.

Pamai Tech
Turning ideas into Reality

Products

Office Add-in

Enterprise Solutions

Cloud Consulting

UI UX Design

Data Transformation

Services

FAQ's

Privacy Policy

Terms & Condition

Team

Contact Us

Company

About Us

Services

Features

Our Pricing

Latest News

© 2023 Pamai Tech