Skip to content

Commit 0fb210e

Browse files
[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]>
1 parent 1cc1897 commit 0fb210e

File tree

5 files changed

+84
-10
lines changed

5 files changed

+84
-10
lines changed

docs/images/data-validation-error.png

16.4 KB
Loading
5.54 KB
Loading

docs/resources/samples/data-validation-samples.md

Lines changed: 81 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
---
2-
title: Create a dropdown list using data validation
2+
title: "Data validation: dropdown lists, prompts, and warning pop-ups"
33
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
55
ms.localizationpriority: medium
66
---
77

8-
# Create a dropdown list using data validation
8+
# Data validation: dropdown lists, prompts, and warning pop-ups
99

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.
1115

1216
:::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.":::
1317

1418
```TypeScript
1519
function main(workbook: ExcelScript.Workbook) {
1620
// 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();
1923

2024
// Convert the values into a comma-delimited string.
2125
let dataValidationListString = "";
@@ -29,8 +33,8 @@ function main(workbook: ExcelScript.Workbook) {
2933
selectedRange.clear(ExcelScript.ClearApplyTo.contents);
3034

3135
// Apply the data validation to the first cell in the selected range.
32-
let targetCell = selectedRange.getCell(0,0);
33-
let dataValidation = targetCell.getDataValidation();
36+
const targetCell = selectedRange.getCell(0,0);
37+
const dataValidation = targetCell.getDataValidation();
3438

3539
// Set the content of the dropdown list.
3640
dataValidation.setRule({
@@ -41,3 +45,72 @@ function main(workbook: ExcelScript.Workbook) {
4145
});
4246
}
4347
```
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.
61+
const selectedSheet = workbook.getActiveWorksheet();
62+
const dataValidation = selectedSheet.getRange("C2:C8").getDataValidation();
63+
64+
// 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.
88+
*/
89+
function main(workbook: ExcelScript.Workbook) {
90+
// Get the range B2:B5 in the active worksheet.
91+
const currentSheet = workbook.getActiveWorksheet();
92+
const positiveNumberOnlyCells = currentSheet.getRange("B2:B5");
93+
94+
// Create a data validation rule to only allow positive numbers.
95+
const positiveNumberValidation: ExcelScript.BasicDataValidation = {
96+
formula1: "0",
97+
operator: ExcelScript.DataValidationOperator.greaterThan
98+
};
99+
const positiveNumberOnlyRule: ExcelScript.DataValidationRule = {
100+
wholeNumber: positiveNumberValidation
101+
};
102+
103+
// Set the rule on the range.
104+
const rangeDataValidation = positiveNumberOnlyCells.getDataValidation();
105+
rangeDataValidation.setRule(positiveNumberOnlyRule);
106+
107+
// Create an alert to appear when data other than positive numbers are entered.
108+
const positiveNumberOnlyAlert: ExcelScript.DataValidationErrorAlert = {
109+
message: "Positive numbers only.",
110+
showAlert: true,
111+
style: ExcelScript.DataValidationAlertStyle.stop,
112+
title: "Invalid data"
113+
};
114+
rangeDataValidation.setErrorAlert(positiveNumberOnlyAlert);
115+
}
116+
```

docs/resources/samples/samples-overview.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Office Scripts samples
33
description: Available Office Scripts samples and scenarios.
4-
ms.date: 09/08/2023
4+
ms.date: 09/20/2023
55
ms.localizationpriority: medium
66
---
77

@@ -26,6 +26,7 @@ This section contains [Office Scripts](../../overview/excel.md) based solutions
2626
| [Add comments in Excel](add-excel-comments.md) | This sample adds comments to a cell including @mentioning a colleague. |
2727
| [Add images to a workbook](add-image-to-workbook.md) | This sample adds an image to a workbook and copies an image across sheets.|
2828
| [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. |
2930
| [Create a workbook table of contents](table-of-contents.md) | This sample creates a table of contents with links to each worksheet. |
3031
| [JavaScript `Date` samples](javascript-dates.md) | A collection of samples that show how to translate between JavaScript and Excel date formats. |
3132
| [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. |

docs/toc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ items:
7878
href: resources/samples/add-image-to-workbook.md
7979
- name: Copy multiple tables into single table
8080
href: resources/samples/copy-tables-combine.md
81-
- name: Create a drop-down list using data validation
81+
- name: "Data validation: dropdown lists, prompts, and warning pop-ups"
8282
href: resources/samples/data-validation-samples.md
8383
- name: Create a workbook table of contents
8484
href: resources/samples/table-of-contents.md

0 commit comments

Comments
 (0)