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
[excel] (Data Validation) Adding more data validation samples (#652)
* Adding more data validation samples
* Apply suggestions from code review
Co-authored-by: Elizabeth Samuel <[email protected]>
---------
Co-authored-by: Elizabeth Samuel <[email protected]>
Copy file name to clipboardExpand all lines: docs/resources/samples/data-validation-samples.md
+81-8Lines changed: 81 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,21 +1,25 @@
1
1
---
2
-
title: Create a dropdown list using data validation
2
+
title: "Data validation: dropdown lists, prompts, and warning pop-ups"
3
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
4
+
ms.date: 09/20/2023
5
5
ms.localizationpriority: medium
6
6
---
7
7
8
-
# Create a dropdown list using data validation
8
+
# Data validation: dropdown lists, prompts, and warning pop-ups
9
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.
10
+
Data validation helps the user ensure consistency in a worksheet. Use these features to limit what can be entered into a cell and provide warnings or errors to users when those conditions aren't met. To learn more about data validation in Excel, see [Apply data validation to cells](https://support.microsoft.com/office/29fecbcc-d1b9-42c1-9d76-eff3ce5f7249).
11
+
12
+
## Create a dropdown list using data validation
13
+
14
+
The following sample creates a dropdown selection list for a cell. It uses the existing values of the selected range as the choices for the list.
11
15
12
16
:::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
17
14
18
```TypeScript
15
19
function main(workbook:ExcelScript.Workbook) {
16
20
// Get the values for data validation.
17
-
let selectedRange =workbook.getSelectedRange();
18
-
let rangeValues =selectedRange.getValues();
21
+
const selectedRange =workbook.getSelectedRange();
22
+
const rangeValues =selectedRange.getValues();
19
23
20
24
// Convert the values into a comma-delimited string.
21
25
let dataValidationListString ="";
@@ -29,8 +33,8 @@ function main(workbook: ExcelScript.Workbook) {
@@ -41,3 +45,72 @@ function main(workbook: ExcelScript.Workbook) {
41
45
});
42
46
}
43
47
```
48
+
49
+
## Add a prompt to a range
50
+
51
+
This example creates a prompt note that appears when a user enters the given cells. This is used to remind users about input requirements, without strict enforcement.
52
+
53
+
:::image type="content" source="../../images/data-validation-prompt.png" alt-text="A prompt with the title 'First names only' and the message 'Only enter the first name of the employee, not the full name.' next to a worksheet with some names in cells.":::
54
+
55
+
```TypeScript
56
+
/**
57
+
* This script creates a text prompt that's shown in C2:C8 when a user enters the cell.
58
+
*/
59
+
function main(workbook:ExcelScript.Workbook) {
60
+
// Get the data validation object for C2:C8 in the current worksheet.
// Clear any previous validation to avoid conflicts.
65
+
dataValidation.clear();
66
+
67
+
// Create a prompt to remind users to only enter first names in this column.
68
+
const prompt:ExcelScript.DataValidationPrompt= {
69
+
showPrompt: true,
70
+
title: "First names only",
71
+
message: "Only enter the first name of the employee, not the full name."
72
+
}
73
+
dataValidation.setPrompt(prompt);
74
+
}
75
+
```
76
+
77
+
## Alert the user when invalid data is entered
78
+
79
+
The following sample script prevents the user from entering anything other than positive numbers into a range. If they try to put anything else, an error message pops up and indicates the problem.
80
+
81
+
:::image type="content" source="../../images/data-validation-error.png" alt-text="An error message with the title 'Invalid data' and the message 'Positive numbers only.' next to a cell with a negative number.":::
82
+
83
+
```TypeScript
84
+
/**
85
+
* This script creates a data validation rule for the range B2:B5.
86
+
* All values in that range must be a positive number.
87
+
* Attempts to enter other values are blocked and an error message appears.
Copy file name to clipboardExpand all lines: docs/resources/samples/samples-overview.md
+2-1Lines changed: 2 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: Office Scripts samples
3
3
description: Available Office Scripts samples and scenarios.
4
-
ms.date: 09/08/2023
4
+
ms.date: 09/20/2023
5
5
ms.localizationpriority: medium
6
6
---
7
7
@@ -26,6 +26,7 @@ This section contains [Office Scripts](../../overview/excel.md) based solutions
26
26
|[Add comments in Excel](add-excel-comments.md)| This sample adds comments to a cell including @mentioning a colleague. |
27
27
|[Add images to a workbook](add-image-to-workbook.md)| This sample adds an image to a workbook and copies an image across sheets.|
28
28
|[Copy multiple Excel tables into a single table](copy-tables-combine.md)| This sample combines data from multiple Excel tables into a single table that includes all the rows. |
29
+
|[Data validation: dropdown lists, prompts, and warning pop-ups](data-validation-samples.md)| These samples show how to use data validation to mandate specific conditions for cell data and how the user is alerted to these rules. |
29
30
|[Create a workbook table of contents](table-of-contents.md)| This sample creates a table of contents with links to each worksheet. |
30
31
|[JavaScript `Date` samples](javascript-dates.md)| A collection of samples that show how to translate between JavaScript and Excel date formats. |
31
32
|[Record day-to-day changes in Excel and report them with a Power Automate flow](report-day-to-day-changes.md)| This sample uses a scheduled Power Automate flow to record daily readings and report the changes. |
0 commit comments