Skip to content

Commit 75d8fbf

Browse files
authored
Merge pull request #638 from OfficeDev/main
[admin] Publish
2 parents 75b7f7d + 5b15a07 commit 75d8fbf

9 files changed

+146
-70
lines changed

docs/develop/power-automate-integration.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Learn the details of how to pass data to and from your scripts with the followin
5353

5454
- Learn by doing with the [Pass data to scripts in an automatically-run Power Automate flow](../tutorials/excel-power-automate-trigger.md) and [Return data from a script to an automatically-run Power Automate flow](../tutorials/excel-power-automate-returns.md) tutorials.
5555
- Try the [Automated task reminders](../resources/scenarios/task-reminders.md) sample scenario to see everything in action.
56-
- Read [Script parameter and return types in Power Automate](power-automate-parameters-returns.md) for more usage scenarios and the technical TypeScript details.
56+
- Read [Pass data to and from scripts in Power Automate](power-automate-parameters-returns.md) for more usage scenarios and the technical TypeScript details.
5757

5858
## Example
5959

@@ -85,7 +85,7 @@ function main(
8585
## See also
8686

8787
- [Call scripts from a manual Power Automate flow](../tutorials/excel-power-automate-manual.md)
88-
- [Script parameter and return types in Power Automate](power-automate-parameters-returns.md)
88+
- [Pass data to and from scripts in Power Automate](power-automate-parameters-returns.md)
8989
- [Troubleshooting information for Power Automate with Office Scripts](../testing/power-automate-troubleshooting.md)
9090
- [Get started with Power Automate](/power-automate/getting-started)
9191
- [Excel Online (Business) connector reference documentation](/connectors/excelonlinebusiness/)
Lines changed: 10 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
2-
title: Script parameter and return types in Power Automate
3-
description: How and why to pass data to and from Office Scripts with Power Automate.
4-
ms.date: 12/19/2022
2+
title: Pass data to and from scripts in Power Automate
3+
description: How and why to use parameters and return values in Office Scripts with Power Automate.
4+
ms.date: 08/15/2023
55
ms.localizationpriority: medium
66
---
77

8-
# Script parameter and return types in Power Automate
8+
# Pass data to and from scripts in Power Automate
99

1010
Power Automate chains together separate programs into a single automated workflow. Each connector has different parameters it accepts and different values it returns. Your scripts can be written to expand the "Run script" Power Automate action to get additional input or give output.
1111

@@ -14,23 +14,15 @@ Input for your script is specified by adding parameters to the `main` function.
1414
> [!NOTE]
1515
> When you create a "Run script" block in your flow, the accepted parameters and returned types are populated. If you change the parameters or return types of your script, you'll need to redo the "Run script" block of your flow. This ensures the data is being parsed correctly.
1616
17-
## `main` parameters: Pass data to a script
17+
## Pass data to scripts with parameters
1818

19-
All script input is specified as additional parameters for the `main` function. New parameters are added after the mandatory `workbook: ExcelScript.Workbook` parameter. For example, if you wanted a script to accept a `string` that represents a name as input, you would change the `main` signature to `function main(workbook: ExcelScript.Workbook, name: string)`.
19+
Add parameters to scripts to provide input from other parts of the flow. It's the same process to add parameters for flow-based scripts as it is for scripts run through the Excel client. Learn about providing input to scripts in [Get user input for scripts](user-input.md).
2020

21-
### Optional parameters
22-
23-
Optional parameters don't need a value in the flow. They are denoted in your script with the [optional modifier](https://www.typescriptlang.org/docs/handbook/2/functions.html#optional-parameters) `?` (for example, in `function main(workbook: ExcelScript.Workbook, Name?: string)` the parameter `Name` is optional).
24-
25-
### Default parameter values
26-
27-
[Default parameter values](https://www.typescriptlang.org/docs/handbook/variable-declarations.html#default-values) automatically fill the action's field with a value. They also let the script run in Excel without external input. To set a default value, assign a value to the parameter in the `main` signature. For example, in `function main(workbook: ExcelScript.Workbook, location: string = "Seattle")` the parameter `location` has the value `"Seattle"` unless the flow provides something else.
21+
The following screenshot shows what a script with the signature `function main(workbook: ExcelScript.Workbook, location: string = "Seattle")` would display.
2822

2923
:::image type="content" source="../images/power-automate-default-parameter.png" alt-text="The Run script action showing an additional parameter field called 'Location' with a pre-populated value of 'Seattle'.":::
3024

31-
### Drop-down lists for parameters
32-
33-
Help others using your script in their flow by providing a list of acceptable parameter choices. If there is a small subset of values that your script uses, create a parameter that is those literal values. Do this by declaring the parameter type to be a [union of literal values](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types). For example, in `function main(workbook: ExcelScript.Workbook, location: "Seattle" | "Redmond")` the parameter `location` can only be `"Seattle"` or `"Redmond"`. When displayed in Power Automate, users get a drop-down list with those two options.
25+
The [dropdown menus created by type unions](user-input.md#dropdown-lists-for-parameters) also function the same in Power Automate.
3426

3527
:::image type="content" source="../images/power-automate-drop-down-parameter-choices.png" alt-text="The Run script action showing an additional parameter field called 'Location' with choices between 'Seattle' and 'Redmond'.":::
3628

@@ -42,41 +34,11 @@ Returned values are shown as dynamic content from the Run script action in the f
4234

4335
:::image type="content" source="../images/power-automate-return-dynamic-content.png" alt-text="The dynamic content selector in Power Automate showing an entry from a Run script action named 'result'.":::
4436

45-
## Type restrictions
46-
47-
When adding input parameters and return values, consider the following allowances and restrictions.
48-
49-
1. The first parameter must be of type `ExcelScript.Workbook`. Its parameter name doesn't matter.
50-
51-
1. The types `string`, `number`, `boolean`, `unknown`, `object`, and `undefined` are supported.
52-
53-
1. Arrays (both `[]` and `Array<T>` styles) of the previously listed types are supported. Nested arrays are also supported.
54-
55-
1. Union types are allowed if they are a union of literals belonging to a single type (such as `"Left" | "Right"`, not `"Left", 5`). Unions of a supported type with undefined are also supported (such as `string | undefined`).
56-
57-
1. Object types are allowed if they contain properties of type `string`, `number`, `boolean`, supported arrays, or other supported objects. The following example shows nested objects that are supported as parameter types.
58-
59-
```TypeScript
60-
// The Employee object is supported because Position is also composed of supported types.
61-
interface Employee {
62-
name: string;
63-
job: Position;
64-
}
65-
66-
interface Position {
67-
id: number;
68-
title: string;
69-
}
70-
```
71-
72-
1. Objects must have their interface or class definition defined in the script. An object can also be defined anonymously inline, as in the following example.
73-
74-
```TypeScript
75-
function main(workbook: ExcelScript.Workbook): {name: string, email: string}
76-
```
37+
Acceptable types for returning data are the same as for parameters. Details on type restrictions are found in the article [Get user input for scripts](user-input.md#type-restrictions).
7738

7839
## See also
7940

8041
- [Call scripts from a manual Power Automate flow](../tutorials/excel-power-automate-manual.md)
42+
- [Get user input for scripts](user-input.md)
8143
- [Run Office Scripts with Power Automate](power-automate-integration.md)
8244
- [Troubleshooting information for Power Automate with Office Scripts](../testing/power-automate-troubleshooting.md)

docs/develop/user-input.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---
2+
title: Get user input for scripts
3+
description: Add parameters to Office Scripts so users can control their experience.
4+
ms.date: 08/22/2023
5+
ms.localizationpriority: medium
6+
---
7+
8+
# Get user input for scripts
9+
10+
Adding parameters to your script lets other users provide data for the script, without needing to edit code. When your script is run through the ribbon or a button, a prompt pops up that asks for input.
11+
12+
:::image type="content" source="../images/user-input-example.png" alt-text="The dialog box shown to users when a script with parameters is run.":::
13+
14+
> [!IMPORTANT]
15+
> Currently, only select users in preview will be prompted to enter data for parameterized scripts in Excel on the web. Power Automate flows also support giving data to scripts through parameters.
16+
17+
## Example - Highlight large values
18+
19+
The following example shows a script that takes a number and string from the user. To test it, open an empty workbook and enter some numbers into several cells.
20+
21+
```TypeScript
22+
/**
23+
* This script applies a background color to cells over a certain value.
24+
* @param highlightThreshold The value used for comparisons.
25+
* @param color A string representing the color to make the high value cells.
26+
* This must be a color code representing the color of the background,
27+
* in the form #RRGGBB (e.g., "FFA500") or a named HTML color (e.g., "orange").
28+
*/
29+
function main(
30+
workbook: ExcelScript.Workbook,
31+
highlightThreshold: number,
32+
color: string) {
33+
// Get the used cells in the current worksheet.
34+
const currentSheet = workbook.getActiveWorksheet();
35+
const usedRange = currentSheet.getUsedRange();
36+
37+
const rangeValues = usedRange.getValues();
38+
for (let row = 0; row < rangeValues.length; row++) {
39+
for (let column = 0; column < rangeValues[row].length; column++) {
40+
if (rangeValues[row][column] >= highlightThreshold) {
41+
usedRange.getCell(row, column).getFormat().getFill().setColor(color);
42+
}
43+
}
44+
}
45+
}
46+
```
47+
48+
## `main` parameters: Pass data to a script
49+
50+
All script input is specified as additional parameters for the `main` function. New parameters are added after the mandatory `workbook: ExcelScript.Workbook` parameter. For example, if you wanted a script to accept a `string` that represents a name as input, you would change the `main` signature to `function main(workbook: ExcelScript.Workbook, name: string)`.
51+
52+
### Optional parameters
53+
54+
Optional parameters don't need the user to provide a value. This implies your script either has default behavior or this parameter is only needed in a corner case. They're denoted in your script with the [optional modifier](https://www.typescriptlang.org/docs/handbook/2/functions.html#optional-parameters) `?`. For example, in `function main(workbook: ExcelScript.Workbook, Name?: string)` the parameter `Name` is optional.
55+
56+
### Default parameter values
57+
58+
[Default parameter values](https://www.typescriptlang.org/docs/handbook/variable-declarations.html#default-values) automatically fill the action's field with a value. To set a default value, assign a value to the parameter in the `main` signature. For example, in `function main(workbook: ExcelScript.Workbook, location: string = "Seattle")` the parameter `location` has the value `"Seattle"` unless something else is provided.
59+
60+
### Dropdown lists for parameters
61+
62+
Help others using your script in their flow by providing a list of acceptable parameter choices. If there's a small subset of values that your script uses, create a parameter that is those literal values. Do this by declaring the parameter type to be a [union of literal values](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types). For example, in `function main(workbook: ExcelScript.Workbook, location: "Seattle" | "Redmond")` the parameter `location` can only be `"Seattle"` or `"Redmond"`. When the script is run, users get a dropdown list with those two options.
63+
64+
### Document the script
65+
66+
Code comments that follow [JSDoc](https://en.wikipedia.org/wiki/JSDoc) standards will be shown to people when they run your script. The more details you put in the descriptions, the easier it'll be for others to the scripts. Describe the purpose of each input parameter and any restrictions or limits. The following sample JSDoc shows how to document a script with a `number` parameter called `taxRate`.
67+
68+
```TypeScript
69+
/**
70+
* A script to apply the current tax rate to sales figures.
71+
* @param taxRate The current sales tax rate in the region as a decimal number (enter 12% as .12).
72+
*/
73+
function main(workbook: ExcelScript.Workbook, taxRate: number)
74+
```
75+
76+
> [!NOTE]
77+
> You don't need to document the `ExcelScript.Workbook` parameter in every script.
78+
79+
## Type restrictions
80+
81+
When adding input parameters and return values, consider the following allowances and restrictions.
82+
83+
1. The first parameter must be of type `ExcelScript.Workbook`. Its parameter name doesn't matter.
84+
85+
1. The types `string`, `number`, `boolean`, `unknown`, and `object`.
86+
87+
1. Arrays (both `[]` and `Array<T>` styles) of the previously listed types are supported. Nested arrays are also supported.
88+
89+
1. Union types are allowed if they're a union of literals belonging to a single type (such as `"Left" | "Right"`, not `"Left" | 5`).
90+
91+
1. Object types are allowed if they contain properties of type `string`, `number`, `boolean`, supported arrays, or other supported objects. The following example shows nested objects that are supported as parameter types.
92+
93+
```TypeScript
94+
// The Employee object is supported because Position is also composed of supported types.
95+
interface Employee {
96+
name: string;
97+
job: Position;
98+
}
99+
100+
interface Position {
101+
id: number;
102+
title: string;
103+
}
104+
```
105+
106+
1. Objects must have their interface or class definition defined in the script. An object can also be defined anonymously inline, as in the following example.
107+
108+
```TypeScript
109+
function main(workbook: ExcelScript.Workbook, contact: {name: string, email: string})
110+
```
111+
112+
## See also
113+
114+
- [Pass data to and from scripts in Power Automate](power-automate-parameters-returns.md)
115+
- [Run Office Scripts in Excel with buttons](script-buttons.md)

docs/images/user-input-example.png

58.8 KB
Loading
Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Add comments in Excel
33
description: Learn how to use Office Scripts to add comments in a worksheet.
4-
ms.date: 06/29/2021
4+
ms.date: 08/14/2023
55
ms.localizationpriority: medium
66
---
77

@@ -23,55 +23,52 @@ This sample shows how to add comments to a cell including [@mentioning](https://
2323

2424
## Sample Excel file
2525

26-
Download [excel-comments.xlsx](excel-comments.xlsx) for a ready-to-use workbook. Add the following script to try the sample yourself!
26+
Download [add-excel-comments.xlsx](add-excel-comments.xlsx) for a ready-to-use workbook. Add the following script to try the sample yourself!
2727

2828
## Sample code: Add comments
2929

3030
```TypeScript
3131
function main(workbook: ExcelScript.Workbook) {
3232
// Get the list of employees.
3333
const employees = workbook.getWorksheet('Employees').getUsedRange().getTexts();
34-
console.log(employees);
35-
34+
3635
// Get the schedule information from the schedule table.
3736
const scheduleSheet = workbook.getWorksheet('Schedule');
3837
const table = scheduleSheet.getTables()[0];
3938
const range = table.getRangeBetweenHeaderAndTotal();
4039
const scheduleData = range.getTexts();
4140

41+
// Find old comments, so we can delete them later.
42+
const oldCommentAddresses = scheduleSheet.getComments().map(oldComment => oldComment.getLocation().getAddress());
43+
4244
// Look through the schedule for a matching employee.
4345
for (let i = 0; i < scheduleData.length; i++) {
44-
let employeeId = scheduleData[i][3];
46+
const employeeId = scheduleData[i][3];
4547

4648
// Compare the employee ID in the schedule against the employee information table.
47-
let employeeInfo = employees.find(employeeRow => employeeRow[0] === employeeId);
49+
const employeeInfo = employees.find(employeeRow => employeeRow[0] === employeeId);
4850
if (employeeInfo) {
49-
console.log("Found a match " + employeeInfo);
50-
let adminNotes = scheduleData[i][4];
51+
const adminNotes = scheduleData[i][4];
52+
const commentCell = range.getCell(i, 5);
5153

52-
// Look for and delete old comments, so we avoid conflicts.
53-
let comment = workbook.getCommentByCell(range.getCell(i, 5));
54-
if (comment) {
54+
// Delete old comments, so we avoid conflicts.
55+
if (oldCommentAddresses.find(oldCommentAddress => oldCommentAddress === commentCell.getAddress())) {
56+
const comment = workbook.getCommentByCell(commentCell);
5557
comment.delete();
5658
}
5759

5860
// Add a comment using the admin notes as the text.
59-
workbook.addComment(range.getCell(i,5), {
61+
workbook.addComment(commentCell, {
6062
mentions: [{
6163
email: employeeInfo[1],
6264
id: 0, // This ID maps this mention to the `id=0` text in the comment.
6365
name: employeeInfo[2]
6466
}],
6567
richContent: `<at id=\"0\">${employeeInfo[2]}</at> ${adminNotes}`
66-
}, ExcelScript.ContentType.mention);
67-
68+
}, ExcelScript.ContentType.mention);
6869
} else {
6970
console.log("No match for: " + employeeId);
7071
}
7172
}
7273
}
7374
```
74-
75-
## Training video: Add comments
76-
77-
[Watch Sudhi Ramamurthy walk through this sample on YouTube](https://youtu.be/CpR78nkaOFw).
12.6 KB
Binary file not shown.
-11.7 KB
Binary file not shown.

docs/testing/power-automate-troubleshooting.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Power Automate allows users to pass arrays to connectors as a variable or as sin
7575

7676
Excel files don't have an inherent location or timezone. Every time a user opens the workbook, their session uses that user's local timezone for date calculations. Power Automate always uses UTC.
7777

78-
If your script uses dates or times, there may be behavioral differences when the script is tested locally versus when it is run through Power Automate. Power Automate allows you to convert, format, and adjust times. See [Working with Dates and Times inside of your flows](https://make.powerautomate.com/blog/working-with-dates-and-times/) for instructions on how to use those functions in Power Automate and [Script parameter and return types in Power Automate](../develop/power-automate-parameters-returns.md) to learn how to provide that time information for the script.
78+
If your script uses dates or times, there may be behavioral differences when the script is tested locally versus when it is run through Power Automate. Power Automate allows you to convert, format, and adjust times. See [Working with Dates and Times inside of your flows](https://make.powerautomate.com/blog/working-with-dates-and-times/) for instructions on how to use those functions in Power Automate and [Pass data to and from scripts in Power Automate](../develop/power-automate-parameters-returns.md) to learn how to provide that time information for the script.
7979

8080
## Script parameter fields or returned output not appearing in Power Automate
8181

docs/toc.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ items:
3434
href: develop/external-calls.md
3535
- name: File storage and ownership
3636
href: overview/script-storage.md
37-
- name: Parameter and return types in Power Automate
37+
- name: Get user input for scripts
38+
href: develop/user-input.md
39+
- name: Pass data in Power Automate
3840
href: develop/power-automate-parameters-returns.md
3941
- name: Run scripts with buttons
4042
href: develop/script-buttons.md

0 commit comments

Comments
 (0)