Skip to content

Commit bd4eaca

Browse files
authored
Add table and list validation samples (#310)
1 parent 3583568 commit bd4eaca

File tree

6 files changed

+426
-29
lines changed

6 files changed

+426
-29
lines changed

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,47 @@ properties:
5959
content: 'list?: ListDataValidation;'
6060
return:
6161
type: '<xref uid="ExcelScript!ExcelScript.ListDataValidation:interface" />'
62+
description: |-
63+
64+
65+
#### Examples
66+
67+
```TypeScript
68+
/**
69+
* This script creates a dropdown selection list for a cell.
70+
* It uses the existing values of the selected range as the choices for the list.
71+
*/
72+
function main(workbook: ExcelScript.Workbook) {
73+
// Get the values for data validation.
74+
const selectedRange = workbook.getSelectedRange();
75+
const rangeValues = selectedRange.getValues();
76+
77+
// Convert the values into a comma-delimited string.
78+
let dataValidationListString = "";
79+
rangeValues.forEach((rangeValueRow) => {
80+
rangeValueRow.forEach((value) => {
81+
dataValidationListString += value + ",";
82+
});
83+
});
84+
85+
// Clear the old range.
86+
selectedRange.clear(ExcelScript.ClearApplyTo.contents);
87+
88+
// Apply the data validation to the first cell in the selected range.
89+
const targetCell = selectedRange.getCell(0, 0);
90+
const dataValidation = targetCell.getDataValidation();
91+
92+
// Set the content of the dropdown list.
93+
let validationCriteria : ExcelScript.ListDataValidation = {
94+
inCellDropDown: true,
95+
source: dataValidationListString
96+
};
97+
let validationRule: ExcelScript.DataValidationRule = {
98+
list: validationCriteria
99+
};
100+
dataValidation.setRule(validationRule);
101+
}
102+
```
62103
- name: textLength
63104
uid: 'ExcelScript!ExcelScript.DataValidationRule#textLength:member'
64105
package: ExcelScript!

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,47 @@ uid: 'ExcelScript!ExcelScript.ListDataValidation:interface'
44
package: ExcelScript!
55
fullName: ExcelScript.ListDataValidation
66
summary: Represents the List data validation criteria.
7-
remarks: ''
7+
remarks: |-
8+
9+
10+
#### Examples
11+
12+
```TypeScript
13+
/**
14+
* This script creates a dropdown selection list for a cell.
15+
* It uses the existing values of the selected range as the choices for the list.
16+
*/
17+
function main(workbook: ExcelScript.Workbook) {
18+
// Get the values for data validation.
19+
const selectedRange = workbook.getSelectedRange();
20+
const rangeValues = selectedRange.getValues();
21+
22+
// Convert the values into a comma-delimited string.
23+
let dataValidationListString = "";
24+
rangeValues.forEach((rangeValueRow) => {
25+
rangeValueRow.forEach((value) => {
26+
dataValidationListString += value + ",";
27+
});
28+
});
29+
30+
// Clear the old range.
31+
selectedRange.clear(ExcelScript.ClearApplyTo.contents);
32+
33+
// Apply the data validation to the first cell in the selected range.
34+
const targetCell = selectedRange.getCell(0, 0);
35+
const dataValidation = targetCell.getDataValidation();
36+
37+
// Set the content of the dropdown list.
38+
let validationCriteria : ExcelScript.ListDataValidation = {
39+
inCellDropDown: true,
40+
source: dataValidationListString
41+
};
42+
let validationRule: ExcelScript.DataValidationRule = {
43+
list: validationCriteria
44+
};
45+
dataValidation.setRule(validationRule);
46+
}
47+
```
848
isPreview: false
949
isDeprecated: false
1050
type: interface

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

Lines changed: 123 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,28 @@ methods:
9999
type: '(boolean | string | number)[]'
100100
return:
101101
type: void
102-
description: ''
102+
description: |-
103+
104+
105+
#### Examples
106+
107+
```TypeScript
108+
/**
109+
* This script adds a row to an existing table.
110+
*/
111+
function main(workbook: ExcelScript.Workbook) {
112+
// Get the first table in the current worksheet.
113+
const selectedSheet = workbook.getActiveWorksheet();
114+
const table = selectedSheet.getTables()[0];
115+
116+
// Initialize the data to be added as a table row.
117+
// Note that length of the array must match the number of columns in the table.
118+
let rowData = ["Carrots", "Vegetable", 750];
119+
120+
// Add a row to the end of the table.
121+
table.addRow(-1, rowData);
122+
}
123+
```
103124
- name: 'addRows(index, values)'
104125
uid: 'ExcelScript!ExcelScript.Table#addRows:member(1)'
105126
package: ExcelScript!
@@ -121,7 +142,30 @@ methods:
121142
type: '(boolean | string | number)[][]'
122143
return:
123144
type: void
124-
description: ''
145+
description: |-
146+
147+
148+
#### Examples
149+
150+
```TypeScript
151+
/**
152+
* This script adds multiple rows to an existing table.
153+
*/
154+
function main(workbook: ExcelScript.Workbook) {
155+
// Get the first table in the current worksheet.
156+
const selectedSheet = workbook.getActiveWorksheet();
157+
const table = selectedSheet.getTables()[0];
158+
159+
// Initialize the data to be added as table rows.
160+
// Note that length of the array must match the number of columns in the table.
161+
let rowData = [["Apples", "Fruit", 5000],
162+
["Celery", "Vegetable", 600],
163+
["Onions", "Vegetable", 1500]];
164+
165+
// Add the rows to the end of the table.
166+
table.addRows(-1, rowData);
167+
}
168+
```
125169
- name: clearFilters()
126170
uid: 'ExcelScript!ExcelScript.Table#clearFilters:member(1)'
127171
package: ExcelScript!
@@ -147,7 +191,27 @@ methods:
147191
content: 'convertToRange(): Range;'
148192
return:
149193
type: '<xref uid="ExcelScript!ExcelScript.Range:interface" />'
150-
description: ''
194+
description: |-
195+
196+
197+
#### Examples
198+
199+
```TypeScript
200+
/**
201+
* This script converts a table to a range and removes the formatting.
202+
*/
203+
function main(workbook: ExcelScript.Workbook) {
204+
// Get the first table in the current worksheet.
205+
const selectedSheet = workbook.getActiveWorksheet();
206+
const table = selectedSheet.getTables()[0];
207+
208+
// Convert the table to a range.
209+
const formerTable = table.convertToRange();
210+
211+
// Remove the formatting from the table
212+
formerTable.clear(ExcelScript.ClearApplyTo.formats);
213+
}
214+
```
151215
- name: delete()
152216
uid: 'ExcelScript!ExcelScript.Table#delete:member(1)'
153217
package: ExcelScript!
@@ -160,7 +224,24 @@ methods:
160224
content: 'delete(): void;'
161225
return:
162226
type: void
163-
description: ''
227+
description: |-
228+
229+
230+
#### Examples
231+
232+
```TypeScript
233+
/**
234+
* This script deletes a table.
235+
* This removes all associated data and formatting.
236+
*/
237+
function main(workbook: ExcelScript.Workbook) {
238+
// Get the table named "Inventory".
239+
const table = workbook.getTable("Inventory");
240+
241+
// Delete the table.
242+
table.delete();
243+
}
244+
```
164245
- name: 'deleteRowsAt(index, count)'
165246
uid: 'ExcelScript!ExcelScript.Table#deleteRowsAt:member(1)'
166247
package: ExcelScript!
@@ -269,7 +350,26 @@ methods:
269350
type: string
270351
return:
271352
type: '<xref uid="ExcelScript!ExcelScript.TableColumn:interface" /> | undefined'
272-
description: ''
353+
description: |-
354+
355+
356+
#### Examples
357+
358+
```TypeScript
359+
/**
360+
* This script removes a specific column from a table.
361+
*/
362+
function main(workbook: ExcelScript.Workbook) {
363+
// Get the table named "Inventory".
364+
const table = workbook.getTable("Inventory");
365+
366+
// If it exists, remove the column named "Category".
367+
let categoryColumn = table.getColumnByName("Category");
368+
if (categoryColumn) {
369+
categoryColumn.delete();
370+
}
371+
}
372+
```
273373
- name: getColumns()
274374
uid: 'ExcelScript!ExcelScript.Table#getColumns:member(1)'
275375
package: ExcelScript!
@@ -809,4 +909,21 @@ methods:
809909
type: boolean
810910
return:
811911
type: void
812-
description: ''
912+
description: |-
913+
914+
915+
#### Examples
916+
917+
```TypeScript
918+
/**
919+
* This script adds the Total Row to an existing table.
920+
*/
921+
function main(workbook: ExcelScript.Workbook) {
922+
// Get the first table in the current worksheet.
923+
const selectedSheet = workbook.getActiveWorksheet();
924+
const table = selectedSheet.getTables()[0];
925+
926+
// Set the Total Row to show.
927+
table.setShowTotals(true);
928+
}
929+
```

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,26 @@ methods:
2121
content: 'delete(): void;'
2222
return:
2323
type: void
24-
description: ''
24+
description: |-
25+
26+
27+
#### Examples
28+
29+
```TypeScript
30+
/**
31+
* This script removes a specific column from a table.
32+
*/
33+
function main(workbook: ExcelScript.Workbook) {
34+
// Get the table named "Inventory".
35+
const table = workbook.getTable("Inventory");
36+
37+
// If it exists, remove the column named "Category".
38+
let categoryColumn = table.getColumnByName("Category");
39+
if (categoryColumn) {
40+
categoryColumn.delete();
41+
}
42+
}
43+
```
2544
- name: getFilter()
2645
uid: 'ExcelScript!ExcelScript.TableColumn#getFilter:member(1)'
2746
package: ExcelScript!

0 commit comments

Comments
 (0)