Skip to content

Commit ec97ed2

Browse files
authored
Merge pull request #579 from OfficeDev/main
[admin] Publish
2 parents 2935a04 + 9f0c367 commit ec97ed2

File tree

4 files changed

+192
-36
lines changed

4 files changed

+192
-36
lines changed
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
| | Excel on the web | Excel for Windows | Excel for Mac | Excel for iOS | Other Office products | Power Automate |
22
|-|-|-|-|-|-|-|
3-
| **VBA macros** | No | Yes | Yes | No | Yes | No |
43
| **Office Scripts** | Yes | Yes | Yes | No | No | Yes |
5-
| **Office Scripts Action Recorder** | Yes | No | No | No | No | No |
4+
| **Office Scripts Action Recorder** | Yes | No | No | No | No | No |
5+
| **VBA macros** | No | Yes | Yes | No | Yes | No |
6+
| **Office Add-ins** | Yes | Yes | Yes | Yes | Yes | No |
7+
| **COM Add-ins** | No | Yes | Yes | No | Yes | No |

docs/resources/add-ins-differences.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Differences between Office Scripts and Office Add-ins
33
description: The behavior and API differences between Office Scripts and Office Add-ins.
4-
ms.date: 10/01/2022
4+
ms.date: 02/13/2023
55
ms.localizationpriority: medium
66
---
77

docs/resources/samples/excel-samples.md

Lines changed: 186 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Basic scripts for Office Scripts in Excel
33
description: A collection of code samples to use with Office Scripts in Excel.
4-
ms.date: 06/24/2022
4+
ms.date: 02/13/2023
55
ms.localizationpriority: medium
66
---
77

@@ -50,6 +50,99 @@ function main(workbook: ExcelScript.Workbook) {
5050
}
5151
```
5252

53+
### Add data to a range
54+
55+
This script adds a set of values to a new worksheet. The values start in cell **A1**. The data used in this script is predefined, but could be sourced from other places in or out of the workbook.
56+
57+
```TypeScript
58+
function main(workbook: ExcelScript.Workbook) {
59+
// The getData call could be replaced by input from Power Automate or a fetch call.
60+
const data = getData();
61+
62+
// Create a new worksheet and switch to it.
63+
const newWorksheet = workbook.addWorksheet("DataSheet");
64+
newWorksheet.activate();
65+
66+
// Get a range matching the size of the data.
67+
const dataRange = newWorksheet.getRangeByIndexes(
68+
0,
69+
0,
70+
data.length,
71+
data[0].length);
72+
73+
// Set the data as the values in the range.
74+
dataRange.setValues(data);
75+
}
76+
77+
function getData(): string[][] {
78+
return [["Abbreviation", "State/Province", "Country"],
79+
["AL", "Alabama", "USA"],
80+
["AK", "Alaska", "USA"],
81+
["AZ", "Arizona", "USA"],
82+
["AR", "Arkansas", "USA"],
83+
["CA", "California", "USA"],
84+
["CO", "Colorado", "USA"],
85+
["CT", "Connecticut", "USA"],
86+
["DE", "Delaware", "USA"],
87+
["DC", "District of Columbia", "USA"],
88+
["FL", "Florida", "USA"],
89+
["GA", "Georgia", "USA"],
90+
["HI", "Hawaii", "USA"],
91+
["ID", "Idaho", "USA"],
92+
["IL", "Illinois", "USA"],
93+
["IN", "Indiana", "USA"],
94+
["IA", "Iowa", "USA"],
95+
["KS", "Kansas", "USA"],
96+
["KY", "Kentucky", "USA"],
97+
["LA", "Louisiana", "USA"],
98+
["ME", "Maine", "USA"],
99+
["MD", "Maryland", "USA"],
100+
["MA", "Massachusetts", "USA"],
101+
["MI", "Michigan", "USA"],
102+
["MN", "Minnesota", "USA"],
103+
["MS", "Mississippi", "USA"],
104+
["MO", "Missouri", "USA"],
105+
["MT", "Montana", "USA"],
106+
["NE", "Nebraska", "USA"],
107+
["NV", "Nevada", "USA"],
108+
["NH", "New Hampshire", "USA"],
109+
["NJ", "New Jersey", "USA"],
110+
["NM", "New Mexico", "USA"],
111+
["NY", "New York", "USA"],
112+
["NC", "North Carolina", "USA"],
113+
["ND", "North Dakota", "USA"],
114+
["OH", "Ohio", "USA"],
115+
["OK", "Oklahoma", "USA"],
116+
["OR", "Oregon", "USA"],
117+
["PA", "Pennsylvania", "USA"],
118+
["RI", "Rhode Island", "USA"],
119+
["SC", "South Carolina", "USA"],
120+
["SD", "South Dakota", "USA"],
121+
["TN", "Tennessee", "USA"],
122+
["TX", "Texas", "USA"],
123+
["UT", "Utah", "USA"],
124+
["VT", "Vermont", "USA"],
125+
["VA", "Virginia", "USA"],
126+
["WA", "Washington", "USA"],
127+
["WV", "West Virginia", "USA"],
128+
["WI", "Wisconsin", "USA"],
129+
["WY", "Wyoming", "USA"],
130+
["AB", "Alberta", "CAN"],
131+
["BC", "British Columbia", "CAN"],
132+
["MB", "Manitoba", "CAN"],
133+
["NB", "New Brunswick", "CAN"],
134+
["NL", "Newfoundland and Labrador", "CAN"],
135+
["NT", "Northwest Territory", "CAN"],
136+
["NS", "Nova Scotia", "CAN"],
137+
["NU", "Nunavut Territory", "CAN"],
138+
["ON", "Ontario", "CAN"],
139+
["PE", "Prince Edward Island", "CAN"],
140+
["QC", "Quebec", "CAN"],
141+
["SK", "Saskatchewan", "CAN"],
142+
["YT", "Yukon Territory", "CAN"]];
143+
}
144+
```
145+
53146
### Change an adjacent cell
54147

55148
This script gets adjacent cells using relative references. Note that if the active cell is on the top row, part of the script fails, because it references the cell above the currently selected one.
@@ -146,11 +239,31 @@ function main(workbook: ExcelScript.Workbook) {
146239
}
147240
```
148241

149-
### Unhide All Rows and Columns
242+
## Row and column visibility
243+
244+
These samples demonstrate how to show, hide, and freeze rows and columns.
245+
246+
### Hide columns
247+
248+
This script hides columns "D", "F", and "J".
249+
250+
```TypeScript
251+
function main(workbook: ExcelScript.Workbook) {
252+
// Get the current worksheet.
253+
const sheet = workbook.getActiveWorksheet();
254+
255+
// Hide columns D, F, and J.
256+
sheet.getRange("D:D").setColumnHidden(true);
257+
sheet.getRange("F:F").setColumnHidden(true);
258+
sheet.getRange("J:J").setColumnHidden(true);
259+
}
260+
```
261+
262+
### Show all rows and columns
150263

151-
This script get the worksheet's used range, checks if there are any hidden rows and columns, and unhides them.
264+
This script get the worksheet's used range, checks if there are any hidden rows and columns, and shows them.
152265

153-
```Typescript
266+
```TypeScript
154267
function main(workbook: ExcelScript.Workbook) {
155268
// Get the currently selected sheet.
156269
const selectedSheet = workbook.getActiveWorksheet();
@@ -164,14 +277,14 @@ function main(workbook: ExcelScript.Workbook) {
164277
return;
165278
}
166279

167-
// If no columns are hidden, log message, else, unhide columns
280+
// If no columns are hidden, log message, else show columns.
168281
if (range.getColumnHidden() == false) {
169282
console.log(`No columns hidden`);
170283
} else {
171284
range.setColumnHidden(false);
172285
}
173286

174-
// If no rows are hidden, log message, else, unhide rows.
287+
// If no rows are hidden, log message, else, show rows.
175288
if (range.getRowHidden() == false) {
176289
console.log(`No rows hidden`);
177290
} else {
@@ -180,7 +293,7 @@ function main(workbook: ExcelScript.Workbook) {
180293
}
181294
```
182295

183-
### Freeze Currently Selected Cells
296+
### Freeze currently selected cells
184297

185298
This script checks what cells are currently selected and freezes that selection, so those cells are always visible.
186299

@@ -303,31 +416,9 @@ function main(workbook: ExcelScript.Workbook) {
303416
}
304417
```
305418

306-
## Display data
419+
## Tables
307420

308-
These samples demonstrate how to work with worksheet data and provide users with a better view or organization.
309-
310-
### Apply conditional formatting
311-
312-
This sample applies conditional formatting to the currently used range in the worksheet. The conditional formatting is a green fill for the top 10% of values.
313-
314-
```TypeScript
315-
function main(workbook: ExcelScript.Workbook) {
316-
// Get the current worksheet.
317-
let selectedSheet = workbook.getActiveWorksheet();
318-
319-
// Get the used range in the worksheet.
320-
let range = selectedSheet.getUsedRange();
321-
322-
// Set the fill color to green for the top 10% of values in the range.
323-
let conditionalFormat = range.addConditionalFormat(ExcelScript.ConditionalFormatType.topBottom)
324-
conditionalFormat.getTopBottom().getFormat().getFill().setColor("green");
325-
conditionalFormat.getTopBottom().setRule({
326-
rank: 10, // The percentage threshold.
327-
type: ExcelScript.ConditionalTopBottomCriterionType.topPercent // The type of the top/bottom condition.
328-
});
329-
}
330-
```
421+
The samples in this section showcase common interactions with Excel tables.
331422

332423
### Create a sorted table
333424

@@ -368,10 +459,73 @@ function main(workbook: ExcelScript.Workbook) {
368459
> [!TIP]
369460
> Copy the filtered information across the workbook by using `Range.copyFrom`. Add the following line to the end of the script to create a new worksheet with the filtered data.
370461
>
371-
> ```typescript
462+
> ```TypeScript
372463
> workbook.addWorksheet().getRange("A1").copyFrom(table.getRange());
373464
> ```
374465
466+
### Dynamically reference table values
467+
468+
This script uses the "@COLUMN_NAME" syntax to set formulas in a table column. The column names in the table can be changed without changing this script.
469+
470+
```TypeScript
471+
function main(workbook: ExcelScript.Workbook) {
472+
// Get the current worksheet.
473+
const table = workbook.getTable("Profits");
474+
475+
// Get the column names for columns 2 and 3.
476+
// Note that these are 1-based indices.
477+
const nameOfColumn2 = table.getColumn(2).getName();
478+
const nameOfColumn3 = table.getColumn(3).getName();
479+
480+
// Set the formula of the fourth column to be the product of the values found
481+
// in that row's second and third columns.
482+
const combinedColumn = table.getColumn(4).getRangeBetweenHeaderAndTotal();
483+
combinedColumn.setFormula(`=[@[${nameOfColumn2}]]*[@[${nameOfColumn3}]]`);
484+
}
485+
```
486+
487+
#### Before the script
488+
489+
| Month | Price | Units Sold | Total |
490+
|--|--|--|--|
491+
| Jan | 45 | 5 | |
492+
| Feb | 45 | 3 | |
493+
| Mar | 45 | 6 | |
494+
495+
#### After the script
496+
497+
| Month | Price | Units Sold | Total |
498+
|--|--|--|--|
499+
| Jan | 45 | 5 | 225 |
500+
| Feb | 45 | 3 | 135 |
501+
| Mar | 45 | 6 | 270 |
502+
503+
## Display data
504+
505+
These samples demonstrate how to work with worksheet data and provide users with a better view or organization.
506+
507+
### Apply conditional formatting
508+
509+
This sample applies conditional formatting to the currently used range in the worksheet. The conditional formatting is a green fill for the top 10% of values.
510+
511+
```TypeScript
512+
function main(workbook: ExcelScript.Workbook) {
513+
// Get the current worksheet.
514+
let selectedSheet = workbook.getActiveWorksheet();
515+
516+
// Get the used range in the worksheet.
517+
let range = selectedSheet.getUsedRange();
518+
519+
// Set the fill color to green for the top 10% of values in the range.
520+
let conditionalFormat = range.addConditionalFormat(ExcelScript.ConditionalFormatType.topBottom)
521+
conditionalFormat.getTopBottom().getFormat().getFill().setColor("green");
522+
conditionalFormat.getTopBottom().setRule({
523+
rank: 10, // The percentage threshold.
524+
type: ExcelScript.ConditionalTopBottomCriterionType.topPercent // The type of the top/bottom condition.
525+
});
526+
}
527+
```
528+
375529
### Log the "Grand Total" values from a PivotTable
376530

377531
This 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).

docs/resources/vba-differences.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Differences between Office Scripts and VBA macros
33
description: The behavior and API differences between Office Scripts and Excel VBA macros.
4-
ms.date: 10/01/2022
4+
ms.date: 02/13/2023
55
ms.localizationpriority: medium
66
---
77

0 commit comments

Comments
 (0)