Skip to content

Commit ac35a31

Browse files
authored
Merge pull request #645 from OfficeDev/main
[admin] Publish
2 parents 639188d + 0d76b21 commit ac35a31

20 files changed

+732
-777
lines changed

.openpublishing.redirection.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@
22
"redirections": [
33
{
44
"source_path": "docs/resources/excel-samples.md",
5-
"redirect_url": "/office/dev/scripts/resources/samples/excel-samples"
5+
"redirect_url": "/office/dev/scripts/resources/samples/samples-overview"
6+
},
7+
{
8+
"source_path": "docs/resources/samples/excel-samples.md",
9+
"redirect_url": "/office/dev/scripts/resources/samples/samples-overview"
10+
},
11+
{
12+
"source_path": "docs/resources/samples/clear-table-filter-for-active-cell.md",
13+
"redirect_url": "/office/dev/scripts/resources/samples/table-samples"
614
},
715
{
816
"source_path": "docs/resources/samples/document-number-generator.md",

docs/develop/pivottables.md

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
2-
title: 'Work with PivotTables in Office Scripts'
3-
description: 'Learn about the object model for PivotTables in the Office Scripts JavaScript API.'
4-
ms.date: 10/01/2022
2+
title: Work with PivotTables in Office Scripts
3+
description: Learn about the object model for PivotTables in the Office Scripts JavaScript API.
4+
ms.date: 09/08/2023
55
ms.localizationpriority: medium
66
---
77

@@ -74,6 +74,36 @@ Each part of the PivotTable maps to a range. This lets your script get data from
7474

7575
:::image type="content" source="../images/pivottable-layout-breakdown.png" alt-text="A diagram showing which sections of a PivotTable are returned by the layout's get range functions.":::
7676

77+
### PivotTable total output
78+
79+
The location of the total row is based on the layout. Use [`PivotLayout.getBodyAndTotalRange`](/javascript/api/office-scripts/excelscript/excelscript.pivotlayout#excelscript-excelscript-pivotlayout-getbodyandtotalrange-member(1)) and get the last row of the column to use the data from the PivotTable in your script.
80+
81+
The following sample finds the first PivotTable in the workbook and logs the values in the "Grand Total" cells (as highlighted in green in the image below).
82+
83+
:::image type="content" source="../images/sample-pivottable-grand-total-row.png" alt-text="A PivotTable showing fruit sales with the Grand Total row highlighted green.":::
84+
85+
```TypeScript
86+
function main(workbook: ExcelScript.Workbook) {
87+
// Get the first PivotTable in the workbook.
88+
const pivotTable = workbook.getPivotTables()[0];
89+
90+
// Get the names of each data column in the PivotTable.
91+
const pivotColumnLabelRange = pivotTable.getLayout().getColumnLabelRange();
92+
93+
// Get the range displaying the pivoted data.
94+
const pivotDataRange = pivotTable.getLayout().getBodyAndTotalRange();
95+
96+
// Get the range with the "grand totals" for the PivotTable columns.
97+
const grandTotalRange = pivotDataRange.getLastRow();
98+
99+
// Print each of the "Grand Totals" to the console.
100+
grandTotalRange.getValues()[0].forEach((column, columnIndex) => {
101+
console.log(`Grand total of ${pivotColumnLabelRange.getValues()[0][columnIndex]}: ${grandTotalRange.getValues()[0][columnIndex]}`);
102+
// Example log: "Grand total of Sum of Crates Sold Wholesale: 11000"
103+
});
104+
}
105+
```
106+
77107
## Filters and slicers
78108

79109
There are three ways to filter a PivotTable.

docs/images/write-large-dataset-1.png

20.4 KB
Loading

docs/images/write-large-dataset-2.png

14.8 KB
Loading

docs/images/write-large-dataset-3.png

12.2 KB
Loading

docs/images/write-large-dataset-4.png

31.6 KB
Loading

docs/images/write-large-dataset-5.png

35.3 KB
Loading

docs/images/write-large-dataset-6.png

14.4 KB
Loading

docs/images/write-large-dataset-7.png

9.76 KB
Loading

docs/images/write-large-dataset-8.png

22.1 KB
Loading

0 commit comments

Comments
 (0)