Skip to content

Commit b073cbc

Browse files
authored
[excel] (Formatting, Worksheet) Add search and custom format samples (#355)
* Add conditional formatting and worksheet search samples * Run generate docs
1 parent 58906f9 commit b073cbc

File tree

6 files changed

+122
-24
lines changed

6 files changed

+122
-24
lines changed

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,32 @@ uid: 'ExcelScript!ExcelScript.ConditionalFormatRule:interface'
44
package: ExcelScript!
55
fullName: ExcelScript.ConditionalFormatRule
66
summary: 'Represents a rule, for all traditional rule/format pairings.'
7-
remarks: ''
7+
remarks: |-
8+
9+
10+
#### Examples
11+
12+
```TypeScript
13+
/**
14+
* This script applies a custom conditional formatting to the selected range.
15+
* A light-green fill is applied to a cell if the value is larger than the value in the row's previous column.
16+
*/
17+
function main(workbook: ExcelScript.Workbook) {
18+
// Get the selected cells.
19+
let selectedRange = workbook.getSelectedRange();
20+
21+
// Apply a rule for positive change from the previous column.
22+
let positiveChange: ExcelScript.ConditionalFormat = selectedRange.addConditionalFormat(ExcelScript.ConditionalFormatType.custom);
23+
24+
// Set the conditional format to be a lightgreen fill.
25+
let positiveCustom: ExcelScript.CustomConditionalFormat = positiveChange.getCustom();
26+
positiveCustom.getFormat().getFill().setColor("lightgreen");
27+
28+
// Set the conditional rule to be if there is positive change across the row.
29+
let positiveRule: ExcelScript.ConditionalFormatRule = positiveCustom.getRule();
30+
positiveRule.setFormula(`=${selectedRange.getCell(0, 0).getAddress()}>${selectedRange.getOffsetRange(0, -1).getCell(0, 0).getAddress()}`);
31+
}
32+
```
833
isPreview: false
934
isDeprecated: false
1035
type: interface

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,22 @@ remarks: |-
1919
let selectedRange = workbook.getSelectedRange();
2020
2121
// Apply a rule for positive change from the previous column.
22-
let positiveChange = selectedRange.addConditionalFormat(ExcelScript.ConditionalFormatType.custom);
23-
positiveChange.getCustom().getFormat().getFill().setColor("lightgreen");
24-
positiveChange.getCustom().getRule().setFormula(`=${selectedRange.getCell(0, 0).getAddress()}>${selectedRange.getOffsetRange(0, -1).getCell(0, 0).getAddress()}`);
22+
let positiveChange: ExcelScript.ConditionalFormat = selectedRange.addConditionalFormat(ExcelScript.ConditionalFormatType.custom);
23+
let positiveCustom: ExcelScript.CustomConditionalFormat = positiveChange.getCustom();
24+
positiveCustom.getFormat().getFill().setColor("lightgreen");
25+
positiveCustom.getRule().setFormula(`=${selectedRange.getCell(0, 0).getAddress()}>${selectedRange.getOffsetRange(0, -1).getCell(0, 0).getAddress()}`);
2526
2627
// Apply a rule for negative change from the previous column.
27-
let negativeChange = selectedRange.addConditionalFormat(ExcelScript.ConditionalFormatType.custom);
28-
negativeChange.getCustom().getFormat().getFill().setColor("pink");
29-
negativeChange.getCustom().getRule().setFormula(`=${selectedRange.getCell(0, 0).getAddress()}<${selectedRange.getOffsetRange(0, -1).getCell(0, 0).getAddress()}`);
28+
let negativeChange: ExcelScript.ConditionalFormat = selectedRange.addConditionalFormat(ExcelScript.ConditionalFormatType.custom);
29+
let negativeCustom: ExcelScript.CustomConditionalFormat = negativeChange.getCustom();
30+
negativeCustom.getFormat().getFill().setColor("pink");
31+
negativeCustom.getRule().setFormula(`=${selectedRange.getCell(0, 0).getAddress()}<${selectedRange.getOffsetRange(0, -1).getCell(0, 0).getAddress()}`);
3032
3133
// Apply a rule for no change from the previous column.
32-
let noChange = selectedRange.addConditionalFormat(ExcelScript.ConditionalFormatType.custom);
33-
noChange.getCustom().getFormat().getFill().setColor("lightyellow");
34-
noChange.getCustom().getRule().setFormula(`=${selectedRange.getCell(0, 0).getAddress()}=${selectedRange.getOffsetRange(0, -1).getCell(0, 0).getAddress()}`);
34+
let sameChange: ExcelScript.ConditionalFormat = selectedRange.addConditionalFormat(ExcelScript.ConditionalFormatType.custom);
35+
let sameCustom: ExcelScript.CustomConditionalFormat = sameChange.getCustom();
36+
sameCustom.getFormat().getFill().setColor("lightyellow");
37+
sameCustom.getRule().setFormula(`=${selectedRange.getCell(0, 0).getAddress()}=${selectedRange.getOffsetRange(0, -1).getCell(0, 0).getAddress()}`);
3538
}
3639
```
3740
isPreview: false

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ methods:
792792
```TypeScript
793793
/**
794794
* This script searches through a worksheet and finds cells containing "No".
795-
* Those cells are filled red.
795+
* Those cells are filled with the color red.
796796
* Use Range.find instead of Worksheet.findAll when you want to limit the search to a specific range.
797797
*/
798798
function main(workbook: ExcelScript.Workbook) {

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,32 @@ uid: 'ExcelScript!ExcelScript.WorksheetSearchCriteria:interface'
44
package: ExcelScript!
55
fullName: ExcelScript.WorksheetSearchCriteria
66
summary: Represents the worksheet search criteria to be used.
7-
remarks: ''
7+
remarks: |-
8+
9+
10+
#### Examples
11+
12+
```TypeScript
13+
/**
14+
* This script searches through a worksheet and finds cells containing "No".
15+
* Those cells are filled with the color red.
16+
* Use Range.find instead of Worksheet.findAll when you want to limit the search to a specific range.
17+
*/
18+
function main(workbook: ExcelScript.Workbook) {
19+
// Get the current, active worksheet.
20+
const worksheet = workbook.getActiveWorksheet();
21+
22+
// Get all the cells that exactly contain the string "No".
23+
const searchCriteria: ExcelScript.WorksheetSearchCriteria = {
24+
completeMatch: true,
25+
matchCase: true
26+
};
27+
const noCells = worksheet.findAll("No", searchCriteria);
28+
29+
// Set the fill color to red.
30+
noCells.getFormat().getFill().setColor("red");
31+
}
32+
```
833
isPreview: false
934
isDeprecated: false
1035
type: interface

docs/sample-scripts/excel-scripts.yaml

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,27 @@
12501250
};
12511251
presetFormat.setRule(duplicateRule);
12521252
}
1253+
'ExcelScript.ConditionalFormatRule:interface':
1254+
- |-
1255+
/**
1256+
* This script applies a custom conditional formatting to the selected range.
1257+
* A light-green fill is applied to a cell if the value is larger than the value in the row's previous column.
1258+
*/
1259+
function main(workbook: ExcelScript.Workbook) {
1260+
// Get the selected cells.
1261+
let selectedRange = workbook.getSelectedRange();
1262+
1263+
// Apply a rule for positive change from the previous column.
1264+
let positiveChange: ExcelScript.ConditionalFormat = selectedRange.addConditionalFormat(ExcelScript.ConditionalFormatType.custom);
1265+
1266+
// Set the conditional format to be a lightgreen fill.
1267+
let positiveCustom: ExcelScript.CustomConditionalFormat = positiveChange.getCustom();
1268+
positiveCustom.getFormat().getFill().setColor("lightgreen");
1269+
1270+
// Set the conditional rule to be if there is positive change across the row.
1271+
let positiveRule: ExcelScript.ConditionalFormatRule = positiveCustom.getRule();
1272+
positiveRule.setFormula(`=${selectedRange.getCell(0, 0).getAddress()}>${selectedRange.getOffsetRange(0, -1).getCell(0, 0).getAddress()}`);
1273+
}
12531274
'ExcelScript.ConditionalFormatRuleType:enum':
12541275
- |-
12551276
/**
@@ -1640,19 +1661,22 @@
16401661
let selectedRange = workbook.getSelectedRange();
16411662
16421663
// Apply a rule for positive change from the previous column.
1643-
let positiveChange = selectedRange.addConditionalFormat(ExcelScript.ConditionalFormatType.custom);
1644-
positiveChange.getCustom().getFormat().getFill().setColor("lightgreen");
1645-
positiveChange.getCustom().getRule().setFormula(`=${selectedRange.getCell(0, 0).getAddress()}>${selectedRange.getOffsetRange(0, -1).getCell(0, 0).getAddress()}`);
1664+
let positiveChange: ExcelScript.ConditionalFormat = selectedRange.addConditionalFormat(ExcelScript.ConditionalFormatType.custom);
1665+
let positiveCustom: ExcelScript.CustomConditionalFormat = positiveChange.getCustom();
1666+
positiveCustom.getFormat().getFill().setColor("lightgreen");
1667+
positiveCustom.getRule().setFormula(`=${selectedRange.getCell(0, 0).getAddress()}>${selectedRange.getOffsetRange(0, -1).getCell(0, 0).getAddress()}`);
16461668
16471669
// Apply a rule for negative change from the previous column.
1648-
let negativeChange = selectedRange.addConditionalFormat(ExcelScript.ConditionalFormatType.custom);
1649-
negativeChange.getCustom().getFormat().getFill().setColor("pink");
1650-
negativeChange.getCustom().getRule().setFormula(`=${selectedRange.getCell(0, 0).getAddress()}<${selectedRange.getOffsetRange(0, -1).getCell(0, 0).getAddress()}`);
1670+
let negativeChange: ExcelScript.ConditionalFormat = selectedRange.addConditionalFormat(ExcelScript.ConditionalFormatType.custom);
1671+
let negativeCustom: ExcelScript.CustomConditionalFormat = negativeChange.getCustom();
1672+
negativeCustom.getFormat().getFill().setColor("pink");
1673+
negativeCustom.getRule().setFormula(`=${selectedRange.getCell(0, 0).getAddress()}<${selectedRange.getOffsetRange(0, -1).getCell(0, 0).getAddress()}`);
16511674
16521675
// Apply a rule for no change from the previous column.
1653-
let noChange = selectedRange.addConditionalFormat(ExcelScript.ConditionalFormatType.custom);
1654-
noChange.getCustom().getFormat().getFill().setColor("lightyellow");
1655-
noChange.getCustom().getRule().setFormula(`=${selectedRange.getCell(0, 0).getAddress()}=${selectedRange.getOffsetRange(0, -1).getCell(0, 0).getAddress()}`);
1676+
let sameChange: ExcelScript.ConditionalFormat = selectedRange.addConditionalFormat(ExcelScript.ConditionalFormatType.custom);
1677+
let sameCustom: ExcelScript.CustomConditionalFormat = sameChange.getCustom();
1678+
sameCustom.getFormat().getFill().setColor("lightyellow");
1679+
sameCustom.getRule().setFormula(`=${selectedRange.getCell(0, 0).getAddress()}=${selectedRange.getOffsetRange(0, -1).getCell(0, 0).getAddress()}`);
16561680
}
16571681
'ExcelScript.CustomDataValidation:interface':
16581682
- |-
@@ -6748,7 +6772,7 @@
67486772
- |-
67496773
/**
67506774
* This script searches through a worksheet and finds cells containing "No".
6751-
* Those cells are filled red.
6775+
* Those cells are filled with the color red.
67526776
* Use Range.find instead of Worksheet.findAll when you want to limit the search to a specific range.
67536777
*/
67546778
function main(workbook: ExcelScript.Workbook) {
@@ -7006,4 +7030,25 @@
70067030
70077031
// Apply the given protection options.
70087032
sheetProtection.protect(protectionOptions);
7033+
}
7034+
'ExcelScript.WorksheetSearchCriteria:interface':
7035+
- |-
7036+
/**
7037+
* This script searches through a worksheet and finds cells containing "No".
7038+
* Those cells are filled with the color red.
7039+
* Use Range.find instead of Worksheet.findAll when you want to limit the search to a specific range.
7040+
*/
7041+
function main(workbook: ExcelScript.Workbook) {
7042+
// Get the current, active worksheet.
7043+
const worksheet = workbook.getActiveWorksheet();
7044+
7045+
// Get all the cells that exactly contain the string "No".
7046+
const searchCriteria: ExcelScript.WorksheetSearchCriteria = {
7047+
completeMatch: true,
7048+
matchCase: true
7049+
};
7050+
const noCells = worksheet.findAll("No", searchCriteria);
7051+
7052+
// Set the fill color to red.
7053+
noCells.getFormat().getFill().setColor("red");
70097054
}

generate-docs/API Coverage Report.csv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,7 @@ ExcelScript.ConditionalFormatPresetCriterion,"twoStdDevAboveAverage",EnumField,M
12131213
ExcelScript.ConditionalFormatPresetCriterion,"twoStdDevBelowAverage",EnumField,Missing,false
12141214
ExcelScript.ConditionalFormatPresetCriterion,"uniqueValues",EnumField,Missing,false
12151215
ExcelScript.ConditionalFormatPresetCriterion,"yesterday",EnumField,Missing,false
1216-
ExcelScript.ConditionalFormatRule,N/A,Class,Fine,false
1216+
ExcelScript.ConditionalFormatRule,N/A,Class,Fine,true
12171217
ExcelScript.ConditionalFormatRule,"getFormula()",Method,Poor,false
12181218
ExcelScript.ConditionalFormatRule,"getFormulaLocal()",Method,Poor,false
12191219
ExcelScript.ConditionalFormatRule,"setFormula(formula)",Method,Poor,false
@@ -3340,6 +3340,6 @@ ExcelScript.WorksheetProtectionOptions,"allowInsertRows",Property,Fine,false
33403340
ExcelScript.WorksheetProtectionOptions,"allowPivotTables",Property,Fine,false
33413341
ExcelScript.WorksheetProtectionOptions,"allowSort",Property,Fine,false
33423342
ExcelScript.WorksheetProtectionOptions,"selectionMode",Property,Fine,false
3343-
ExcelScript.WorksheetSearchCriteria,N/A,Class,Fine,false
3343+
ExcelScript.WorksheetSearchCriteria,N/A,Class,Fine,true
33443344
ExcelScript.WorksheetSearchCriteria,"completeMatch",Property,Good,false
33453345
ExcelScript.WorksheetSearchCriteria,"matchCase",Property,Good,false

0 commit comments

Comments
 (0)