Skip to content

Commit 451e0dc

Browse files
authored
Add data bar conditional formatting samples (#389)
1 parent 0777df3 commit 451e0dc

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

docs/sample-scripts/samples.yaml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,48 @@
934934
format.getFill().setColor("yellow");
935935
format.getFont().setItalic(true);
936936
}
937+
'ExcelScript.ConditionalDataBarNegativeFormat:interface':
938+
- |-
939+
/**
940+
* This script applies data bar conditional formatting to a range.
941+
* It sets the data bar colors to be green when positive and red when negative.
942+
*/
943+
function main(workbook: ExcelScript.Workbook) {
944+
// Get the "Sales" data column range in a table named "SalesTable".
945+
const table = workbook.getTable("SalesTable");
946+
const salesRange = table.getColumnByName("Sales").getRangeBetweenHeaderAndTotal();
947+
948+
// Add data bar conditional formatting to the range.
949+
const cf = salesRange.addConditionalFormat(ExcelScript.ConditionalFormatType.dataBar);
950+
951+
// Have the bar show green when positive and red when negative.
952+
const dataBarConditionalFormat = cf.getDataBar();
953+
const positiveFormat: ExcelScript.ConditionalDataBarPositiveFormat = dataBarConditionalFormat.getPositiveFormat();
954+
positiveFormat .setFillColor("green");
955+
const negativeFormat: ExcelScript.ConditionalDataBarNegativeFormat = dataBarConditionalFormat.getNegativeFormat();
956+
negativeFormat.setFillColor("red");
957+
}
958+
'ExcelScript.ConditionalDataBarPositiveFormat:interface':
959+
- |-
960+
/**
961+
* This script applies data bar conditional formatting to a range.
962+
* It sets the data bar colors to be green when positive and red when negative.
963+
*/
964+
function main(workbook: ExcelScript.Workbook) {
965+
// Get the "Sales" data column range in a table named "SalesTable".
966+
const table = workbook.getTable("SalesTable");
967+
const salesRange = table.getColumnByName("Sales").getRangeBetweenHeaderAndTotal();
968+
969+
// Add data bar conditional formatting to the range.
970+
const cf = salesRange.addConditionalFormat(ExcelScript.ConditionalFormatType.dataBar);
971+
972+
// Have the bar show green when positive and red when negative.
973+
const dataBarConditionalFormat = cf.getDataBar();
974+
const positiveFormat: ExcelScript.ConditionalDataBarPositiveFormat = dataBarConditionalFormat.getPositiveFormat();
975+
positiveFormat .setFillColor("green");
976+
const negativeFormat: ExcelScript.ConditionalDataBarNegativeFormat = dataBarConditionalFormat.getNegativeFormat();
977+
negativeFormat.setFillColor("red");
978+
}
937979
'ExcelScript.ConditionalDataBarRule:interface':
938980
- |-
939981
/**

0 commit comments

Comments
 (0)