1250
1250
};
1251
1251
presetFormat.setRule(duplicateRule);
1252
1252
}
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
+ }
1253
1274
' ExcelScript.ConditionalFormatRuleType:enum ' :
1254
1275
- |-
1255
1276
/**
1640
1661
let selectedRange = workbook.getSelectedRange();
1641
1662
1642
1663
// 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()}`);
1646
1668
1647
1669
// 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()}`);
1651
1674
1652
1675
// 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()}`);
1656
1680
}
1657
1681
' ExcelScript.CustomDataValidation:interface ' :
1658
1682
- |-
6748
6772
- |-
6749
6773
/**
6750
6774
* 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.
6752
6776
* Use Range.find instead of Worksheet.findAll when you want to limit the search to a specific range.
6753
6777
*/
6754
6778
function main(workbook: ExcelScript.Workbook) {
7006
7030
7007
7031
// Apply the given protection options.
7008
7032
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");
7009
7054
}
0 commit comments