Skip to content

Commit 0d76b21

Browse files
[excel] (IA) Break out "basic" samples into independent articles (#644)
* Partial refactoring * Refactor basic sample list to increase discoverability * Fix links * Remove view parameter from reference doc link * Apply suggestions from code review Co-authored-by: Elizabeth Samuel <[email protected]> --------- Co-authored-by: Elizabeth Samuel <[email protected]>
1 parent 85914f4 commit 0d76b21

11 files changed

+697
-753
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/resources/samples/clear-table-filter-for-active-cell.md

Lines changed: 0 additions & 56 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
title: Create a dropdown list using data validation
3+
description: Learn how to add data validation to a cell and give the user a selection of values to enter.
4+
ms.date: 09/08/2023
5+
ms.localizationpriority: medium
6+
---
7+
8+
# Create a dropdown list using data validation
9+
10+
This script creates a dropdown selection list for a cell. It uses the existing values of the selected range as the choices for the list.
11+
12+
:::image type="content" source="../../images/sample-data-validation.png" alt-text="A worksheet showing a range of three cells containing color choices 'red, blue, green' and next to it, the same choices shown in a dropdown list.":::
13+
14+
```TypeScript
15+
function main(workbook: ExcelScript.Workbook) {
16+
// Get the values for data validation.
17+
let selectedRange = workbook.getSelectedRange();
18+
let rangeValues = selectedRange.getValues();
19+
20+
// Convert the values into a comma-delimited string.
21+
let dataValidationListString = "";
22+
rangeValues.forEach((rangeValueRow) => {
23+
rangeValueRow.forEach((value) => {
24+
dataValidationListString += value + ",";
25+
});
26+
});
27+
28+
// Clear the old range.
29+
selectedRange.clear(ExcelScript.ClearApplyTo.contents);
30+
31+
// Apply the data validation to the first cell in the selected range.
32+
let targetCell = selectedRange.getCell(0,0);
33+
let dataValidation = targetCell.getDataValidation();
34+
35+
// Set the content of the dropdown list.
36+
dataValidation.setRule({
37+
list: {
38+
inCellDropDown: true,
39+
source: dataValidationListString
40+
}
41+
});
42+
}
43+
```

0 commit comments

Comments
 (0)