|
1 | 1 | ---
|
2 | 2 | title: Work with PivotTables in Office Scripts
|
3 | 3 | description: Learn about the object model for PivotTables in the Office Scripts JavaScript API.
|
4 |
| -ms.date: 10/19/2023 |
| 4 | +ms.date: 04/18/2024 |
5 | 5 | ms.localizationpriority: medium
|
6 | 6 | ---
|
7 | 7 |
|
@@ -179,6 +179,51 @@ The following code snippet adds a slicer for the "Type" field. It sets the selec
|
179 | 179 |
|
180 | 180 | :::image type="content" source="../images/slicer.png" alt-text="A slicer filtering data on a PivotTable.":::
|
181 | 181 |
|
| 182 | +### Value field settings for summaries |
| 183 | + |
| 184 | +Change how the PivotTable summarizes and displays data with these settings. The field in each data hierarchy can display the data in different ways, such as percentages, standard deviations, and relative comparisons. |
| 185 | + |
| 186 | +#### Summarize by |
| 187 | + |
| 188 | +The default summarization of a data hierarchy field is as a sum. `DataPivotHierarchy.setSummarizeBy` lets you combine the data for each row or column in a different way. [`AggregationFunction`](/javascript/api/office-scripts/excelscript/excelscript.aggregationfunction) lists the all the available options. |
| 189 | + |
| 190 | +The following code snippet changes "Crates Sold Wholesale" to show each item's standard deviation, instead of the sum. |
| 191 | + |
| 192 | +```typescript |
| 193 | + const wholesaleSales = farmPivot.getDataHierarchy("Sum of Crates Sold Wholesale"); |
| 194 | + wholesaleSales.setSummarizeBy(ExcelScript.AggregationFunction.standardDeviation); |
| 195 | +``` |
| 196 | + |
| 197 | +#### Show values as |
| 198 | + |
| 199 | +`DataPivotHierarchy.setShowAs` applies a calculation to the values of a data hierarchy. Instead of the default sum, you can show values or percentages relative to other parts of the PivotTable. Use a [`ShowAsRule`](/javascript/api/office-scripts/excelscript/excelscript.showasrule) to set how data hierarchy values are shown. |
| 200 | + |
| 201 | +The following code snippet changes the display for "Crates Sold at Farm". The values will be shown as a percentage of the grand total for the field. |
| 202 | + |
| 203 | +```typescript |
| 204 | + const farmSales = farmPivot.getDataHierarchy("Sum of Crates Sold at Farm"); |
| 205 | + |
| 206 | + const rule : ExcelScript.ShowAsRule = { |
| 207 | + calculation: ExcelScript.ShowAsCalculation.percentOfGrandTotal |
| 208 | + }; |
| 209 | + farmSales.setShowAs(rule); |
| 210 | +``` |
| 211 | + |
| 212 | +Some `ShowAsRule`s need another field or item in that field as a comparison. The following code snippet again changes the display for "Crates Sold at Farm". This time, the field will show each value's difference from the value of the "Lemons" in that farm row. If a farm has not sold any lemons, the field shows "#N/A". |
| 213 | + |
| 214 | +```typescript |
| 215 | + const typeField = farmPivot.getRowHierarchy("Type").getFields()[0]; |
| 216 | + const farmSales = farmPivot.getDataHierarchy("Sum of Crates Sold at Farm"); |
| 217 | + |
| 218 | + const rule: ExcelScript.ShowAsRule = { |
| 219 | + calculation: ExcelScript.ShowAsCalculation.differenceFrom, |
| 220 | + baseField: typeField, /* The field to use for the difference. */ |
| 221 | + baseItem: typeField.getPivotItem("Lemon") /* The item within that field that is the basis of comparison for the difference. */ |
| 222 | + }; |
| 223 | + farmSales.setShowAs(rule); |
| 224 | + farmSales.setName("Difference from Lemons of Crates Sold at Farm"); |
| 225 | +``` |
| 226 | + |
182 | 227 | ## See also
|
183 | 228 |
|
184 | 229 | - [Fundamentals for Office Scripts in Excel](scripting-fundamentals.md)
|
|
0 commit comments