|
1 | 1 | --- |
2 | 2 | title: Get user input for scripts |
3 | 3 | description: Add parameters to Office Scripts so users can control their experience. |
4 | | -ms.date: 09/14/2023 |
| 4 | +ms.date: 08/22/2025 |
5 | 5 | ms.localizationpriority: medium |
6 | 6 | --- |
7 | 7 |
|
8 | 8 | # Get user input for scripts |
9 | 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. |
| 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 the user for input, such as an array or a workbook. |
11 | 11 |
|
12 | 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 | 13 |
|
14 | | -> [!IMPORTANT] |
15 | | -> Currently, only Excel on the web users will be prompted to enter data for parameterized scripts. Power Automate flows also support giving data to scripts through parameters. |
16 | | -
|
17 | | -## Example - Highlight large values |
| 14 | +## Example scenario: Highlight large values |
18 | 15 |
|
19 | 16 | 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 | 17 |
|
@@ -49,6 +46,20 @@ function main( |
49 | 46 |
|
50 | 47 | 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 | 48 |
|
| 49 | +To allow users to import a workbook with a parameterized script, use a two-dimensional array for each parameter that accepts a workbook. The parameter can be of type `string` or `number`. The following example shows how to create a script that accepts workbook imports for both parameters. |
| 50 | + |
| 51 | +```TypeScript |
| 52 | +/** |
| 53 | + * This script generates a monthly sales report. |
| 54 | + * @param productData The product data for this month. |
| 55 | + * @param salesData The sales data for this month. |
| 56 | + */ |
| 57 | +function main(workbook: ExcelScript.Workbook, productData: string[][], salesData: string[][]) { |
| 58 | + // Code to process data goes here. |
| 59 | + // Both the `productData` and `salesData` parameters accept workbook imports. |
| 60 | +} |
| 61 | +``` |
| 62 | + |
52 | 63 | ### Optional parameters |
53 | 64 |
|
54 | 65 | 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. |
|
0 commit comments