Skip to content

Commit 714b0b3

Browse files
authored
Add CustomDataValidation sample (#342)
1 parent 2b1a086 commit 714b0b3

File tree

4 files changed

+96
-3
lines changed

4 files changed

+96
-3
lines changed

docs/docs-ref-autogen/excel/excelscript/excelscript.customdatavalidation.yml

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,32 @@ uid: 'ExcelScript!ExcelScript.CustomDataValidation:interface'
44
package: ExcelScript!
55
fullName: ExcelScript.CustomDataValidation
66
summary: Represents the custom data validation criteria.
7-
remarks: ''
7+
remarks: |-
8+
9+
10+
#### Examples
11+
12+
```TypeScript
13+
/**
14+
* This script adds data validation to a range.
15+
* The validation prevents duplicate entries within that range.
16+
*/
17+
function main(workbook: ExcelScript.Workbook) {
18+
// Get the range "B2:B20".
19+
const sheet = workbook.getActiveWorksheet();
20+
const range = sheet.getRange("B2:B20");
21+
22+
// Set data validation on the range to prevent duplicate, non-blank entries.
23+
const dataValidation = range.getDataValidation();
24+
dataValidation.setIgnoreBlanks(true);
25+
const duplicateRule : ExcelScript.CustomDataValidation = {
26+
formula: "=COUNTIF($B$2:$B$20, B2)=1"
27+
};
28+
dataValidation.setRule({
29+
custom: duplicateRule
30+
});
31+
}
32+
```
833
isPreview: false
934
isDeprecated: false
1035
type: interface

docs/docs-ref-autogen/excel/excelscript/excelscript.datavalidationrule.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,32 @@ properties:
2323
content: 'custom?: CustomDataValidation;'
2424
return:
2525
type: '<xref uid="ExcelScript!ExcelScript.CustomDataValidation:interface" />'
26+
description: |-
27+
28+
29+
#### Examples
30+
31+
```TypeScript
32+
/**
33+
* This script adds data validation to a range.
34+
* The validation prevents duplicate entries within that range.
35+
*/
36+
function main(workbook: ExcelScript.Workbook) {
37+
// Get the range "B2:B20".
38+
const sheet = workbook.getActiveWorksheet();
39+
const range = sheet.getRange("B2:B20");
40+
41+
// Set data validation on the range to prevent duplicate, non-blank entries.
42+
const dataValidation = range.getDataValidation();
43+
dataValidation.setIgnoreBlanks(true);
44+
const duplicateRule : ExcelScript.CustomDataValidation = {
45+
formula: "=COUNTIF($B$2:$B$20, B2)=1"
46+
};
47+
dataValidation.setRule({
48+
custom: duplicateRule
49+
});
50+
}
51+
```
2652
- name: date
2753
uid: 'ExcelScript!ExcelScript.DataValidationRule#date:member'
2854
package: ExcelScript!

docs/sample-scripts/excel-scripts.yaml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,6 +1632,27 @@
16321632
noChange.getCustom().getFormat().getFill().setColor("lightyellow");
16331633
noChange.getCustom().getRule().setFormula(`=${selectedRange.getCell(0, 0).getAddress()}=${selectedRange.getOffsetRange(0, -1).getCell(0, 0).getAddress()}`);
16341634
}
1635+
'ExcelScript.CustomDataValidation:interface':
1636+
- |-
1637+
/**
1638+
* This script adds data validation to a range.
1639+
* The validation prevents duplicate entries within that range.
1640+
*/
1641+
function main(workbook: ExcelScript.Workbook) {
1642+
// Get the range "B2:B20".
1643+
const sheet = workbook.getActiveWorksheet();
1644+
const range = sheet.getRange("B2:B20");
1645+
1646+
// Set data validation on the range to prevent duplicate, non-blank entries.
1647+
const dataValidation = range.getDataValidation();
1648+
dataValidation.setIgnoreBlanks(true);
1649+
const duplicateRule : ExcelScript.CustomDataValidation = {
1650+
formula: "=COUNTIF($B$2:$B$20, B2)=1"
1651+
};
1652+
dataValidation.setRule({
1653+
custom: duplicateRule
1654+
});
1655+
}
16351656
'ExcelScript.DataBarConditionalFormat:interface':
16361657
- |-
16371658
/**
@@ -1897,6 +1918,27 @@
18971918
}
18981919
dataValidation.setPrompt(prompt);
18991920
}
1921+
'ExcelScript.DataValidationRule#custom:member':
1922+
- |-
1923+
/**
1924+
* This script adds data validation to a range.
1925+
* The validation prevents duplicate entries within that range.
1926+
*/
1927+
function main(workbook: ExcelScript.Workbook) {
1928+
// Get the range "B2:B20".
1929+
const sheet = workbook.getActiveWorksheet();
1930+
const range = sheet.getRange("B2:B20");
1931+
1932+
// Set data validation on the range to prevent duplicate, non-blank entries.
1933+
const dataValidation = range.getDataValidation();
1934+
dataValidation.setIgnoreBlanks(true);
1935+
const duplicateRule : ExcelScript.CustomDataValidation = {
1936+
formula: "=COUNTIF($B$2:$B$20, B2)=1"
1937+
};
1938+
dataValidation.setRule({
1939+
custom: duplicateRule
1940+
});
1941+
}
19001942
'ExcelScript.DataValidationRule#list:member':
19011943
- |-
19021944
/**

generate-docs/API Coverage Report.csv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,7 +1328,7 @@ ExcelScript.CultureInfo,"getNumberFormat()",Method,Poor,false
13281328
ExcelScript.CustomConditionalFormat,N/A,Class,Fine,true
13291329
ExcelScript.CustomConditionalFormat,"getFormat()",Method,Poor,false
13301330
ExcelScript.CustomConditionalFormat,"getRule()",Method,Poor,false
1331-
ExcelScript.CustomDataValidation,N/A,Class,Fine,false
1331+
ExcelScript.CustomDataValidation,N/A,Class,Fine,true
13321332
ExcelScript.CustomDataValidation,"formula",Property,Good,false
13331333
ExcelScript.CustomProperty,N/A,Class,Poor,false
13341334
ExcelScript.CustomProperty,"delete()",Method,Poor,false
@@ -1407,7 +1407,7 @@ ExcelScript.DataValidationPrompt,"message",Property,Fine,false
14071407
ExcelScript.DataValidationPrompt,"showPrompt",Property,Fine,false
14081408
ExcelScript.DataValidationPrompt,"title",Property,Fine,false
14091409
ExcelScript.DataValidationRule,N/A,Class,Good,false
1410-
ExcelScript.DataValidationRule,"custom",Property,Poor,false
1410+
ExcelScript.DataValidationRule,"custom",Property,Poor,true
14111411
ExcelScript.DataValidationRule,"date",Property,Poor,false
14121412
ExcelScript.DataValidationRule,"decimal",Property,Poor,false
14131413
ExcelScript.DataValidationRule,"list",Property,Poor,true

0 commit comments

Comments
 (0)