Skip to content

Commit c0aa0a8

Browse files
authored
[excel] (Range, Workbook) Add samples for WorkbookRangeAreas and WorkbookProtection (#338)
* Adding protection samples * Add precedent samples
1 parent 18f0e5f commit c0aa0a8

File tree

6 files changed

+230
-12
lines changed

6 files changed

+230
-12
lines changed

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,30 @@ methods:
799799
content: 'getDirectPrecedents(): WorkbookRangeAreas;'
800800
return:
801801
type: '<xref uid="ExcelScript!ExcelScript.WorkbookRangeAreas:interface" />'
802-
description: ''
802+
description: |-
803+
804+
805+
#### Examples
806+
807+
```TypeScript
808+
/**
809+
* This script finds the direct precedents of the active cell.
810+
* It changes the font and color of those precedent cells.
811+
*/
812+
function main(workbook: ExcelScript.Workbook) {
813+
// Get the active cell.
814+
const selected = workbook.getActiveCell();
815+
816+
// Get the cells that are direct precedents of the current cell.
817+
const precedents : ExcelScript.WorkbookRangeAreas = selected.getDirectPrecedents();
818+
819+
// Set the font to bold and the fill color to orange for all the precedent cells.
820+
precedents.getRanges().forEach(range => {
821+
range.getFormat().getFill().setColor("orange");
822+
range.getFormat().getFont().setBold(true);
823+
});
824+
}
825+
```
803826
- name: getEntireColumn()
804827
uid: 'ExcelScript!ExcelScript.Range#getEntireColumn:member(1)'
805828
package: ExcelScript!

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1259,7 +1259,29 @@ methods:
12591259
content: 'getProtection(): WorkbookProtection;'
12601260
return:
12611261
type: '<xref uid="ExcelScript!ExcelScript.WorkbookProtection:interface" />'
1262-
description: ''
1262+
description: |-
1263+
1264+
1265+
#### Examples
1266+
1267+
```TypeScript
1268+
/**
1269+
* This script protects the workbook with a password, if it isn't already protected.
1270+
* The password is provided by the user through a prompt.
1271+
*/
1272+
function main(workbook: ExcelScript.Workbook, password?: string) {
1273+
// Get the workbook-level protection object.
1274+
const protection = workbook.getProtection();
1275+
1276+
// Check if the workbook is already protected.
1277+
if (!protection.getProtected()) {
1278+
// Protect the workbook with the given password.
1279+
// If the optional password was omitted,
1280+
// no password will be needed to unprotect the workbook.
1281+
protection.protect(password);
1282+
}
1283+
}
1284+
```
12631285
- name: getQueries()
12641286
uid: 'ExcelScript!ExcelScript.Workbook#getQueries:member(1)'
12651287
package: ExcelScript!

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

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,26 @@ methods:
2121
content: 'getProtected(): boolean;'
2222
return:
2323
type: boolean
24-
description: ''
24+
description: |-
25+
26+
27+
#### Examples
28+
29+
```TypeScript
30+
/**
31+
* This script protects the workbook with a default password, if there is not already protection.
32+
*/
33+
function main(workbook: ExcelScript.Workbook) {
34+
// Get the workbook-level protection object.
35+
const protection = workbook.getProtection();
36+
37+
// Check if the workbook is already protected.
38+
if (!protection.getProtected()) {
39+
// Apply a default password.
40+
protection.protect("1234");
41+
}
42+
}
43+
```
2544
- name: protect(password)
2645
uid: 'ExcelScript!ExcelScript.WorkbookProtection#protect:member(1)'
2746
package: ExcelScript!
@@ -38,7 +57,25 @@ methods:
3857
type: string
3958
return:
4059
type: void
41-
description: ''
60+
description: |-
61+
62+
63+
#### Examples
64+
65+
```TypeScript
66+
/**
67+
* This script protects the workbook using a password given in a user prompt.
68+
*/
69+
function main(workbook: ExcelScript.Workbook, password?: string) {
70+
// Get the workbook-level protection object.
71+
const protection = workbook.getProtection();
72+
73+
// Protect the workbook with the given password.
74+
// If the optional password was omitted,
75+
// no password will be needed to unprotect the workbook.
76+
protection.protect(password);
77+
}
78+
```
4279
- name: unprotect(password)
4380
uid: 'ExcelScript!ExcelScript.WorkbookProtection#unprotect:member(1)'
4481
package: ExcelScript!
@@ -55,4 +92,20 @@ methods:
5592
type: string
5693
return:
5794
type: void
58-
description: ''
95+
description: |-
96+
97+
98+
#### Examples
99+
100+
```TypeScript
101+
/**
102+
* This script removes protection from the workbook using a password given in a user prompt.
103+
*/
104+
function main(workbook: ExcelScript.Workbook, password?: string) {
105+
// Get the workbook-level protection object.
106+
const protection = workbook.getProtection();
107+
108+
// Unprotect the workbook with the given password.
109+
protection.unprotect(password);
110+
}
111+
```

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,30 @@ uid: 'ExcelScript!ExcelScript.WorkbookRangeAreas:interface'
44
package: ExcelScript!
55
fullName: ExcelScript.WorkbookRangeAreas
66
summary: Represents a collection of one or more rectangular ranges in multiple worksheets.
7-
remarks: ''
7+
remarks: |-
8+
9+
10+
#### Examples
11+
12+
```TypeScript
13+
/**
14+
* This script finds the direct precedents of the active cell.
15+
* It changes the font and color of those precedent cells.
16+
*/
17+
function main(workbook: ExcelScript.Workbook) {
18+
// Get the active cell.
19+
const selected = workbook.getActiveCell();
20+
21+
// Get the cells that are direct precedents of the current cell.
22+
const precedents : ExcelScript.WorkbookRangeAreas = selected.getDirectPrecedents();
23+
24+
// Set the font to bold and the fill color to orange for all the precedent cells.
25+
precedents.getRanges().forEach(range => {
26+
range.getFormat().getFill().setColor("orange");
27+
range.getFormat().getFont().setBold(true);
28+
});
29+
}
30+
```
831
isPreview: false
932
isDeprecated: false
1033
type: interface

docs/sample-scripts/excel-scripts.yaml

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3864,6 +3864,25 @@
38643864
}
38653865
});
38663866
}
3867+
'ExcelScript.Range#getDirectPrecedents:member(1)':
3868+
- |-
3869+
/**
3870+
* This script finds the direct precedents of the active cell.
3871+
* It changes the font and color of those precedent cells.
3872+
*/
3873+
function main(workbook: ExcelScript.Workbook) {
3874+
// Get the active cell.
3875+
const selected = workbook.getActiveCell();
3876+
3877+
// Get the cells that are direct precedents of the current cell.
3878+
const precedents : ExcelScript.WorkbookRangeAreas = selected.getDirectPrecedents();
3879+
3880+
// Set the font to bold and the fill color to orange for all the precedent cells.
3881+
precedents.getRanges().forEach(range => {
3882+
range.getFormat().getFill().setColor("orange");
3883+
range.getFormat().getFont().setBold(true);
3884+
});
3885+
}
38673886
'ExcelScript.Range#getExtendedRange:member(1)':
38683887
- |-
38693888
/**
@@ -6123,6 +6142,24 @@
61236142
}
61246143
});
61256144
}
6145+
'ExcelScript.Workbook#getProtection:member(1)':
6146+
- |-
6147+
/**
6148+
* This script protects the workbook with a password, if it isn't already protected.
6149+
* The password is provided by the user through a prompt.
6150+
*/
6151+
function main(workbook: ExcelScript.Workbook, password?: string) {
6152+
// Get the workbook-level protection object.
6153+
const protection = workbook.getProtection();
6154+
6155+
// Check if the workbook is already protected.
6156+
if (!protection.getProtected()) {
6157+
// Protect the workbook with the given password.
6158+
// If the optional password was omitted,
6159+
// no password will be needed to unprotect the workbook.
6160+
protection.protect(password);
6161+
}
6162+
}
61266163
'ExcelScript.Workbook#getWorksheet:member(1)':
61276164
- |-
61286165
/**
@@ -6154,6 +6191,66 @@
61546191
console.log(names);
61556192
console.log(`Total worksheets inside of this workbook: ${sheets.length}`);
61566193
}
6194+
'ExcelScript.WorkbookProtection#getProtected:member(1)':
6195+
- |-
6196+
/**
6197+
* This script protects the workbook with a default password, if there is not already protection.
6198+
*/
6199+
function main(workbook: ExcelScript.Workbook) {
6200+
// Get the workbook-level protection object.
6201+
const protection = workbook.getProtection();
6202+
6203+
// Check if the workbook is already protected.
6204+
if (!protection.getProtected()) {
6205+
// Apply a default password.
6206+
protection.protect("1234");
6207+
}
6208+
}
6209+
'ExcelScript.WorkbookProtection#protect:member(1)':
6210+
- |-
6211+
/**
6212+
* This script protects the workbook using a password given in a user prompt.
6213+
*/
6214+
function main(workbook: ExcelScript.Workbook, password?: string) {
6215+
// Get the workbook-level protection object.
6216+
const protection = workbook.getProtection();
6217+
6218+
// Protect the workbook with the given password.
6219+
// If the optional password was omitted,
6220+
// no password will be needed to unprotect the workbook.
6221+
protection.protect(password);
6222+
}
6223+
'ExcelScript.WorkbookProtection#unprotect:member(1)':
6224+
- |-
6225+
/**
6226+
* This script removes protection from the workbook using a password given in a user prompt.
6227+
*/
6228+
function main(workbook: ExcelScript.Workbook, password?: string) {
6229+
// Get the workbook-level protection object.
6230+
const protection = workbook.getProtection();
6231+
6232+
// Unprotect the workbook with the given password.
6233+
protection.unprotect(password);
6234+
}
6235+
'ExcelScript.WorkbookRangeAreas:interface':
6236+
- |-
6237+
/**
6238+
* This script finds the direct precedents of the active cell.
6239+
* It changes the font and color of those precedent cells.
6240+
*/
6241+
function main(workbook: ExcelScript.Workbook) {
6242+
// Get the active cell.
6243+
const selected = workbook.getActiveCell();
6244+
6245+
// Get the cells that are direct precedents of the current cell.
6246+
const precedents : ExcelScript.WorkbookRangeAreas = selected.getDirectPrecedents();
6247+
6248+
// Set the font to bold and the fill color to orange for all the precedent cells.
6249+
precedents.getRanges().forEach(range => {
6250+
range.getFormat().getFill().setColor("orange");
6251+
range.getFormat().getFont().setBold(true);
6252+
});
6253+
}
61576254
'ExcelScript.WorkbookLinksRefreshMode:enum':
61586255
- |-
61596256
/**

generate-docs/API Coverage Report.csv

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2387,7 +2387,7 @@ ExcelScript.Range,"getColumnsBefore(count)",Method,Poor,false
23872387
ExcelScript.Range,"getConditionalFormat(id)",Method,Poor,false
23882388
ExcelScript.Range,"getConditionalFormats()",Method,Poor,false
23892389
ExcelScript.Range,"getDataValidation()",Method,Poor,true
2390-
ExcelScript.Range,"getDirectPrecedents()",Method,Poor,false
2390+
ExcelScript.Range,"getDirectPrecedents()",Method,Fine,true
23912391
ExcelScript.Range,"getEntireColumn()",Method,Poor,false
23922392
ExcelScript.Range,"getEntireRow()",Method,Poor,false
23932393
ExcelScript.Range,"getExtendedRange(direction, activeCell)",Method,Fine,true
@@ -3166,7 +3166,7 @@ ExcelScript.Workbook,"getPredefinedCellStyle(name)",Method,Poor,false
31663166
ExcelScript.Workbook,"getPredefinedCellStyles()",Method,Poor,false
31673167
ExcelScript.Workbook,"getPreviouslySaved()",Method,Poor,false
31683168
ExcelScript.Workbook,"getProperties()",Method,Poor,false
3169-
ExcelScript.Workbook,"getProtection()",Method,Poor,false
3169+
ExcelScript.Workbook,"getProtection()",Method,Poor,true
31703170
ExcelScript.Workbook,"getQueries()",Method,Poor,false
31713171
ExcelScript.Workbook,"getQuery(key)",Method,Poor,false
31723172
ExcelScript.Workbook,"getReadOnly()",Method,Poor,false
@@ -3200,10 +3200,10 @@ ExcelScript.WorkbookLinksRefreshMode,N/A,Enum,Fine,true
32003200
ExcelScript.WorkbookLinksRefreshMode,"automatic",EnumField,Fine,false
32013201
ExcelScript.WorkbookLinksRefreshMode,"manual",EnumField,Fine,false
32023202
ExcelScript.WorkbookProtection,N/A,Class,Fine,false
3203-
ExcelScript.WorkbookProtection,"getProtected()",Method,Poor,false
3204-
ExcelScript.WorkbookProtection,"protect(password)",Method,Poor,false
3205-
ExcelScript.WorkbookProtection,"unprotect(password)",Method,Poor,false
3206-
ExcelScript.WorkbookRangeAreas,N/A,Class,Fine,false
3203+
ExcelScript.WorkbookProtection,"getProtected()",Method,Poor,true
3204+
ExcelScript.WorkbookProtection,"protect(password)",Method,Poor,true
3205+
ExcelScript.WorkbookProtection,"unprotect(password)",Method,Poor,true
3206+
ExcelScript.WorkbookRangeAreas,N/A,Class,Fine,true
32073207
ExcelScript.WorkbookRangeAreas,"getAddresses()",Method,Poor,false
32083208
ExcelScript.WorkbookRangeAreas,"getAreas()",Method,Poor,false
32093209
ExcelScript.WorkbookRangeAreas,"getRangeAreasBySheet(key)",Method,Poor,false

0 commit comments

Comments
 (0)