Skip to content

Commit 2b1a086

Browse files
[excel] (PivotTable) Adding PivotTable summary samples (#340)
* Adding PivotTable summary samples * Fix typos * Apply suggestions from code review Co-authored-by: Alison McKay <[email protected]> --------- Co-authored-by: Alison McKay <[email protected]>
1 parent 8d48e22 commit 2b1a086

File tree

5 files changed

+154
-6
lines changed

5 files changed

+154
-6
lines changed

docs/docs-ref-autogen/excel/excelscript/excelscript.datapivothierarchy.yml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,30 @@ methods:
189189
type: '<xref uid="ExcelScript!ExcelScript.ShowAsRule:interface" />'
190190
return:
191191
type: void
192-
description: ''
192+
description: |-
193+
194+
195+
#### Examples
196+
197+
```TypeScript
198+
/**
199+
* The script changes the display for "Crates Sold at Farm".
200+
* It shows the percentage of the grand total,
201+
* instead of the default sum.
202+
*/
203+
function main(workbook: ExcelScript.Workbook) {
204+
// Get the PivotTable named "Farm Pivot".
205+
const farmPivot = workbook.getPivotTable("Farm Pivot");
206+
207+
// Get the data hierarchy "Sum of Crates Sold at Farm".
208+
const farmSales = farmPivot.getDataHierarchy("Sum of Crates Sold at Farm");
209+
210+
// Show the data as a percentage of the grand total.
211+
farmSales.setShowAs({
212+
calculation: ExcelScript.ShowAsCalculation.percentOfGrandTotal
213+
});
214+
}
215+
```
193216
- name: setSummarizeBy(summarizeBy)
194217
uid: 'ExcelScript!ExcelScript.DataPivotHierarchy#setSummarizeBy:member(1)'
195218
package: ExcelScript!

docs/docs-ref-autogen/excel/excelscript/excelscript.showascalculation.yml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,30 @@ uid: 'ExcelScript!ExcelScript.ShowAsCalculation:enum'
44
package: ExcelScript!
55
fullName: ExcelScript.ShowAsCalculation
66
summary: The ShowAs calculation function for the DataPivotField.
7-
remarks: ''
7+
remarks: |-
8+
9+
10+
#### Examples
11+
12+
```TypeScript
13+
/**
14+
* The script changes the display for "Crates Sold at Farm".
15+
* It shows the percentage of the grand total,
16+
* instead of the default sum.
17+
*/
18+
function main(workbook: ExcelScript.Workbook) {
19+
// Get the PivotTable named "Farm Pivot".
20+
const farmPivot = workbook.getPivotTable("Farm Pivot");
21+
22+
// Get the data hierarchy "Sum of Crates Sold at Farm".
23+
const farmSales = farmPivot.getDataHierarchy("Sum of Crates Sold at Farm");
24+
25+
// Show the data as a percentage of the grand total.
26+
farmSales.setShowAs({
27+
calculation: ExcelScript.ShowAsCalculation.percentOfGrandTotal
28+
});
29+
}
30+
```
831
isPreview: false
932
isDeprecated: false
1033
fields:

docs/docs-ref-autogen/excel/excelscript/excelscript.showasrule.yml

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,41 @@ uid: 'ExcelScript!ExcelScript.ShowAsRule:interface'
44
package: ExcelScript!
55
fullName: ExcelScript.ShowAsRule
66
summary: ''
7-
remarks: ''
7+
remarks: |-
8+
9+
10+
#### Examples
11+
12+
```TypeScript
13+
/**
14+
* The script changes the display for "Crates Sold at Farm".
15+
* The field shows each value's difference
16+
* from the value of the "Lemon" in the same row.
17+
* If the row has no value for "Lemon", the field shows "#N/A".
18+
*/
19+
function main(workbook: ExcelScript.Workbook) {
20+
// Get the PivotTable named "Farm Pivot".
21+
const farmPivot = workbook.getPivotTable("Farm Pivot");
22+
23+
// Get the data hierarchy "Sum of Crates Sold at Farm".
24+
const farmSales = farmPivot.getDataHierarchy("Sum of Crates Sold at Farm");
25+
26+
// Get the row hierarchy "Type".
27+
const typeField = farmPivot.getRowHierarchy("Type").getFields()[0];
28+
29+
// Change the data hierarchy to show each value as the difference
30+
// from the value of the "Lemon" in that row.
31+
const rule: ExcelScript.ShowAsRule = {
32+
calculation: ExcelScript.ShowAsCalculation.differenceFrom,
33+
baseField: typeField,
34+
baseItem: typeField.getPivotItem("Lemon")
35+
}
36+
farmSales.setShowAs(rule);
37+
38+
// Set the name of the field to match the new behavior.
39+
farmSales.setName("Difference from Lemons of Crates Sold at Farm");
40+
}
41+
```
842
isPreview: false
943
isDeprecated: false
1044
type: interface

docs/sample-scripts/excel-scripts.yaml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,6 +1691,25 @@
16911691
const dataHierarchy = pivotTable.getDataHierarchies()[0];
16921692
dataHierarchy.setSummarizeBy(ExcelScript.AggregationFunction.average);
16931693
}
1694+
'ExcelScript.DataPivotHierarchy#setShowAs:member(1)':
1695+
- |-
1696+
/**
1697+
* The script changes the display for "Crates Sold at Farm".
1698+
* It shows the percentage of the grand total,
1699+
* instead of the default sum.
1700+
*/
1701+
function main(workbook: ExcelScript.Workbook) {
1702+
// Get the PivotTable named "Farm Pivot".
1703+
const farmPivot = workbook.getPivotTable("Farm Pivot");
1704+
1705+
// Get the data hierarchy "Sum of Crates Sold at Farm".
1706+
const farmSales = farmPivot.getDataHierarchy("Sum of Crates Sold at Farm");
1707+
1708+
// Show the data as a percentage of the grand total.
1709+
farmSales.setShowAs({
1710+
calculation: ExcelScript.ShowAsCalculation.percentOfGrandTotal
1711+
});
1712+
}
16941713
'ExcelScript.DataValidation#getType:member(1)':
16951714
- |-
16961715
/**
@@ -5293,6 +5312,55 @@
52935312
worksheet.setVisibility(ExcelScript.SheetVisibility.visible);
52945313
});
52955314
}
5315+
'ExcelScript.ShowAsCalculation:enum':
5316+
- |-
5317+
/**
5318+
* The script changes the display for "Crates Sold at Farm".
5319+
* It shows the percentage of the grand total,
5320+
* instead of the default sum.
5321+
*/
5322+
function main(workbook: ExcelScript.Workbook) {
5323+
// Get the PivotTable named "Farm Pivot".
5324+
const farmPivot = workbook.getPivotTable("Farm Pivot");
5325+
5326+
// Get the data hierarchy "Sum of Crates Sold at Farm".
5327+
const farmSales = farmPivot.getDataHierarchy("Sum of Crates Sold at Farm");
5328+
5329+
// Show the data as a percentage of the grand total.
5330+
farmSales.setShowAs({
5331+
calculation: ExcelScript.ShowAsCalculation.percentOfGrandTotal
5332+
});
5333+
}
5334+
'ExcelScript.ShowAsRule:interface':
5335+
- |-
5336+
/**
5337+
* The script changes the display for "Crates Sold at Farm".
5338+
* The field shows each value's difference
5339+
* from the value of the "Lemon" in the same row.
5340+
* If the row has no value for "Lemon", the field shows "#N/A".
5341+
*/
5342+
function main(workbook: ExcelScript.Workbook) {
5343+
// Get the PivotTable named "Farm Pivot".
5344+
const farmPivot = workbook.getPivotTable("Farm Pivot");
5345+
5346+
// Get the data hierarchy "Sum of Crates Sold at Farm".
5347+
const farmSales = farmPivot.getDataHierarchy("Sum of Crates Sold at Farm");
5348+
5349+
// Get the row hierarchy "Type".
5350+
const typeField = farmPivot.getRowHierarchy("Type").getFields()[0];
5351+
5352+
// Change the data hierarchy to show each value as the difference
5353+
// from the value of the "Lemon" in that row.
5354+
const rule: ExcelScript.ShowAsRule = {
5355+
calculation: ExcelScript.ShowAsCalculation.differenceFrom,
5356+
baseField: typeField,
5357+
baseItem: typeField.getPivotItem("Lemon")
5358+
}
5359+
farmSales.setShowAs(rule);
5360+
5361+
// Set the name of the field to match the new behavior.
5362+
farmSales.setName("Difference from Lemons of Crates Sold at Farm");
5363+
}
52965364
'ExcelScript.Slicer:interface':
52975365
- |-
52985366
/**

generate-docs/API Coverage Report.csv

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,7 +1368,7 @@ ExcelScript.DataPivotHierarchy,"getSummarizeBy()",Method,Poor,false
13681368
ExcelScript.DataPivotHierarchy,"setName(name)",Method,Poor,false
13691369
ExcelScript.DataPivotHierarchy,"setNumberFormat(numberFormat)",Method,Poor,false
13701370
ExcelScript.DataPivotHierarchy,"setPosition(position)",Method,Poor,false
1371-
ExcelScript.DataPivotHierarchy,"setShowAs(showAs)",Method,Poor,false
1371+
ExcelScript.DataPivotHierarchy,"setShowAs(showAs)",Method,Poor,true
13721372
ExcelScript.DataPivotHierarchy,"setSummarizeBy(summarizeBy)",Method,Poor,true
13731373
ExcelScript.DataPivotHierarchy,"setToDefault()",Method,Poor,false
13741374
ExcelScript.DataValidation,N/A,Class,Fine,false
@@ -2848,7 +2848,7 @@ ExcelScript.SheetVisibility,N/A,Enum,Missing,true
28482848
ExcelScript.SheetVisibility,"hidden",EnumField,Missing,false
28492849
ExcelScript.SheetVisibility,"veryHidden",EnumField,Missing,false
28502850
ExcelScript.SheetVisibility,"visible",EnumField,Missing,false
2851-
ExcelScript.ShowAsCalculation,N/A,Enum,Fine,false
2851+
ExcelScript.ShowAsCalculation,N/A,Enum,Fine,true
28522852
ExcelScript.ShowAsCalculation,"differenceFrom",EnumField,Fine,false
28532853
ExcelScript.ShowAsCalculation,"index",EnumField,Fine,false
28542854
ExcelScript.ShowAsCalculation,"none",EnumField,Poor,false
@@ -2865,7 +2865,7 @@ ExcelScript.ShowAsCalculation,"rankAscending",EnumField,Fine,false
28652865
ExcelScript.ShowAsCalculation,"rankDecending",EnumField,Fine,false
28662866
ExcelScript.ShowAsCalculation,"runningTotal",EnumField,Fine,false
28672867
ExcelScript.ShowAsCalculation,"unknown",EnumField,Poor,false
2868-
ExcelScript.ShowAsRule,N/A,Class,Missing,false
2868+
ExcelScript.ShowAsRule,N/A,Class,Missing,true
28692869
ExcelScript.ShowAsRule,"baseField",Property,Fine,false
28702870
ExcelScript.ShowAsRule,"baseItem",Property,Fine,false
28712871
ExcelScript.ShowAsRule,"calculation",Property,Good,false

0 commit comments

Comments
 (0)