You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
5
5
ms.localizationpriority: medium
6
6
---
7
7
@@ -50,6 +50,99 @@ function main(workbook: ExcelScript.Workbook) {
50
50
}
51
51
```
52
52
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.
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) {
146
239
}
147
240
```
148
241
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
150
263
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.
@@ -164,14 +277,14 @@ function main(workbook: ExcelScript.Workbook) {
164
277
return;
165
278
}
166
279
167
-
// If no columns are hidden, log message, else, unhide columns
280
+
// If no columns are hidden, log message, else show columns.
168
281
if (range.getColumnHidden() ==false) {
169
282
console.log(`No columns hidden`);
170
283
} else {
171
284
range.setColumnHidden(false);
172
285
}
173
286
174
-
// If no rows are hidden, log message, else, unhide rows.
287
+
// If no rows are hidden, log message, else, show rows.
175
288
if (range.getRowHidden() ==false) {
176
289
console.log(`No rows hidden`);
177
290
} else {
@@ -180,7 +293,7 @@ function main(workbook: ExcelScript.Workbook) {
180
293
}
181
294
```
182
295
183
-
### Freeze Currently Selected Cells
296
+
### Freeze currently selected cells
184
297
185
298
This script checks what cells are currently selected and freezes that selection, so those cells are always visible.
186
299
@@ -303,31 +416,9 @@ function main(workbook: ExcelScript.Workbook) {
303
416
}
304
417
```
305
418
306
-
## Display data
419
+
## Tables
307
420
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)
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.
331
422
332
423
### Create a sorted table
333
424
@@ -368,10 +459,73 @@ function main(workbook: ExcelScript.Workbook) {
368
459
> [!TIP]
369
460
> 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.
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.
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)
0 commit comments