|
| 1 | +--- |
| 2 | +title: Conditional formatting samples |
| 3 | +description: A collection of Office Scripts that use different Excel conditional formatting options. |
| 4 | +ms.date: 12/28/2023 |
| 5 | +ms.localizationpriority: medium |
| 6 | +--- |
| 7 | + |
| 8 | +# Conditional formatting samples |
| 9 | + |
| 10 | +Conditional formatting in Excel applies formatting to cells based on specific conditions or rules. These formats automatically adjust when the data changes, so your script doesn't need to be run multiple times. This page contains a collection of Office Scripts that demonstrate various conditional formatting options. |
| 11 | + |
| 12 | +This sample workbook contains worksheets ready to test with the sample scripts. |
| 13 | + |
| 14 | +> [!div class="nextstepaction"] |
| 15 | +> [Download the sample workbook](conditional-formatting-samples.xlsx) |
| 16 | +
|
| 17 | +## Cell value |
| 18 | + |
| 19 | +[Cell value conditional formatting](/javascript/api/office-scripts/excelscript/excelscript.cellvalueconditionalformat) applies a format to every cell that contains a value meeting a given criteria. This helps quickly spot important data points. |
| 20 | + |
| 21 | +The following sample applies cell value conditional formatting to a range. Any value less than 60 will have the cell's fill color changed and the font made italic. |
| 22 | + |
| 23 | +:::image type="content" source="../../images/conditional-formatting-sample-cell-value.png" alt-text="A list of scores with every cell that contains a value under 60 formatted to have a yellow fill and italic text."::: |
| 24 | + |
| 25 | +```typescript |
| 26 | +function main(workbook: ExcelScript.Workbook) { |
| 27 | + // Get the range to format. |
| 28 | + const sheet = workbook.getWorksheet("CellValue"); |
| 29 | + const ratingColumn = sheet.getRange("B2:B12"); |
| 30 | + sheet.activate(); |
| 31 | + |
| 32 | + // Add cell value conditional formatting. |
| 33 | + const cellValueConditionalFormatting = |
| 34 | + ratingColumn.addConditionalFormat(ExcelScript.ConditionalFormatType.cellValue).getCellValue(); |
| 35 | + |
| 36 | + // Create the condition, in this case when the cell value is less than 60 |
| 37 | + let rule: ExcelScript.ConditionalCellValueRule = { |
| 38 | + formula1: "60", |
| 39 | + operator: ExcelScript.ConditionalCellValueOperator.lessThan |
| 40 | + }; |
| 41 | + cellValueConditionalFormatting.setRule(rule); |
| 42 | + |
| 43 | + // Set the format to apply when the condition is met. |
| 44 | + let format = cellValueConditionalFormatting.getFormat(); |
| 45 | + format.getFill().setColor("yellow"); |
| 46 | + format.getFont().setItalic(true); |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +## Color scale |
| 51 | + |
| 52 | +[Color scale conditional formatting](/javascript/api/office-scripts/excelscript/excelscript.colorscaleconditionalformat) applies a color gradient across a range. The cells with the minimum and maximum values of the range use the colors specified, with other cells scaled proportionally. An optional midpoint color provides more contrast. |
| 53 | + |
| 54 | +This following sample applies a red, white, and blue color scale to the selected range. |
| 55 | + |
| 56 | +:::image type="content" source="../../images/conditional-formatting-sample-color-scale.png" alt-text="A table of temperatures with the lower values colored blue and the higher ones colored red."::: |
| 57 | + |
| 58 | +```typescript |
| 59 | +function main(workbook: ExcelScript.Workbook) { |
| 60 | + // Get the range to format. |
| 61 | + const sheet = workbook.getWorksheet("ColorScale"); |
| 62 | + const dataRange = sheet.getRange("B2:M13"); |
| 63 | + sheet.activate(); |
| 64 | + |
| 65 | + // Create a new conditional formatting object by adding one to the range. |
| 66 | + const conditionalFormatting = dataRange.addConditionalFormat(ExcelScript.ConditionalFormatType.colorScale); |
| 67 | + |
| 68 | + // Set the colors for the three parts of the scale: minimum, midpoint, and maximum. |
| 69 | + conditionalFormatting.getColorScale().setCriteria({ |
| 70 | + minimum: { |
| 71 | + color: "#5A8AC6", /* A pale blue. */ |
| 72 | + type: ExcelScript.ConditionalFormatColorCriterionType.lowestValue |
| 73 | + }, |
| 74 | + midpoint: { |
| 75 | + color: "#FCFCFF", /* Slightly off-white. */ |
| 76 | + formula: '=50', type: ExcelScript.ConditionalFormatColorCriterionType.percentile |
| 77 | + }, |
| 78 | + maximum: { |
| 79 | + color: "#F8696B", /* A pale red. */ |
| 80 | + type: ExcelScript.ConditionalFormatColorCriterionType.highestValue |
| 81 | + } |
| 82 | + }); |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +## Data bar |
| 87 | + |
| 88 | +[Data bar conditional formatting](/javascript/api/office-scripts/excelscript/excelscript.databarconditionalformat) adds a partially-filled bar in the background of a cell. The fullness of the bar is defined by the value in the cell and the range specified by the format. |
| 89 | + |
| 90 | +The following sample creates data bar conditional formatting on the selected ranged. The scale of the data bar goes from 0 to 1200. |
| 91 | + |
| 92 | +:::image type="content" source="../../images/conditional-formatting-sample-data-bar.png" alt-text="A table of values with data bars showing their value compared to 1200."::: |
| 93 | + |
| 94 | +```typescript |
| 95 | + |
| 96 | +function main(workbook: ExcelScript.Workbook) { |
| 97 | + // Get the range to format. |
| 98 | + const sheet = workbook.getWorksheet("DataBar"); |
| 99 | + const dataRange = sheet.getRange("B2:D5"); |
| 100 | + sheet.activate(); |
| 101 | + |
| 102 | + // Create new conditional formatting on the range. |
| 103 | + const format = dataRange.addConditionalFormat(ExcelScript.ConditionalFormatType.dataBar); |
| 104 | + const dataBarFormat = format.getDataBar(); |
| 105 | + |
| 106 | + // Set the lower bound of the data bar formatting to be 0. |
| 107 | + const lowerBound: ExcelScript.ConditionalDataBarRule = { |
| 108 | + type: ExcelScript.ConditionalFormatRuleType.number, |
| 109 | + formula: "0" |
| 110 | + }; |
| 111 | + dataBarFormat.setLowerBoundRule(lowerBound); |
| 112 | + |
| 113 | + // Set the upper bound of the data bar formatting to be 1200. |
| 114 | + const upperBound: ExcelScript.ConditionalDataBarRule = { |
| 115 | + type: ExcelScript.ConditionalFormatRuleType.number, |
| 116 | + formula: "1200" |
| 117 | + }; |
| 118 | + dataBarFormat.setUpperBoundRule(upperBound); |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +## Icon set |
| 123 | + |
| 124 | +[Icon set conditional formatting](/javascript/api/office-scripts/excelscript/excelscript.iconsetconditionalformat) adds icons to each cell in a range. The icons come from a specified set. Icons are applied based on an ordered array of criteria, with each criterion mapping to a single icon. |
| 125 | + |
| 126 | +The following sample applies the "three traffic light" icon set conditional formatting to a range. |
| 127 | + |
| 128 | +:::image type="content" source="../../images/conditional-formatting-sample-icon-set.png" alt-text="A table of scores with red lights next to low values, yellow lights next to medium values, and green lights next to high values."::: |
| 129 | + |
| 130 | +```typescript |
| 131 | +function main(workbook: ExcelScript.Workbook) { |
| 132 | + // Get the range to format. |
| 133 | + const sheet = workbook.getWorksheet("IconSet"); |
| 134 | + const dataRange = sheet.getRange("B2:B12"); |
| 135 | + sheet.activate(); |
| 136 | + |
| 137 | + // Create icon set conditional formatting on the range. |
| 138 | + const conditionalFormatting = dataRange.addConditionalFormat(ExcelScript.ConditionalFormatType.iconSet); |
| 139 | + |
| 140 | + // Use the "3 Traffic Lights (Unrimmed)" set. |
| 141 | + conditionalFormatting.getIconSet().setStyle(ExcelScript.IconSet.threeTrafficLights1); |
| 142 | + conditionalFormatting.getIconSet().setCriteria([ |
| 143 | + { // Use the red light as the default for positive values. |
| 144 | + formula: '=0', operator: ExcelScript.ConditionalIconCriterionOperator.greaterThanOrEqual, |
| 145 | + type: ExcelScript.ConditionalFormatIconRuleType.number |
| 146 | + }, |
| 147 | + { // The yellow light is applied to all values 6 and greater. The replaces the red light when applicable. |
| 148 | + formula: '=6', operator: ExcelScript.ConditionalIconCriterionOperator.greaterThanOrEqual, |
| 149 | + type: ExcelScript.ConditionalFormatIconRuleType.number |
| 150 | + }, |
| 151 | + { // The green light is applied to all values 8 and greater. As with the yellow light, the icon is replaced when the new criteria is met. |
| 152 | + formula: '=8', operator: ExcelScript.ConditionalIconCriterionOperator.greaterThanOrEqual, |
| 153 | + type: ExcelScript.ConditionalFormatIconRuleType.number |
| 154 | + } |
| 155 | + ]); |
| 156 | +} |
| 157 | +``` |
| 158 | + |
| 159 | +## Preset |
| 160 | + |
| 161 | +[Preset conditional formatting](/javascript/api/office-scripts/excelscript/excelscript.presetcriteriaconditionalformat) applies a specified format to a range based on common scenarios, such as blank cells and duplicate values. The full list of preset criteria is provided by the [ConditionalFormatPresetCriterion](/javascript/api/office-scripts/excelscript/excelscript.conditionalformatpresetcriterion) enum. |
| 162 | + |
| 163 | +The following sample gives a yellow fill to any blank cell in the range. |
| 164 | + |
| 165 | +:::image type="content" source="../../images/conditional-formatting-sample-preset.png" alt-text="A table with blank values highlighted with yellow fills."::: |
| 166 | + |
| 167 | +```typescript |
| 168 | +function main(workbook: ExcelScript.Workbook) { |
| 169 | + // Get the range to format. |
| 170 | + const sheet = workbook.getWorksheet("Preset"); |
| 171 | + const dataRange = sheet.getRange("B2:D5"); |
| 172 | + sheet.activate(); |
| 173 | + |
| 174 | + // Add new conditional formatting to that range. |
| 175 | + const conditionalFormat = dataRange.addConditionalFormat( |
| 176 | + ExcelScript.ConditionalFormatType.presetCriteria); |
| 177 | + |
| 178 | + // Set the conditional formatting to apply a yellow fill. |
| 179 | + const presetFormat = conditionalFormat.getPreset(); |
| 180 | + presetFormat.getFormat().getFill().setColor("yellow"); |
| 181 | + |
| 182 | + // Set a rule to apply the conditional format when cells are left blank. |
| 183 | + const blankRule: ExcelScript.ConditionalPresetCriteriaRule = { |
| 184 | + criterion: ExcelScript.ConditionalFormatPresetCriterion.blanks |
| 185 | + }; |
| 186 | + presetFormat.setRule(blankRule); |
| 187 | +} |
| 188 | +``` |
| 189 | + |
| 190 | +## Text comparison |
| 191 | + |
| 192 | +[Text comparison conditional formatting](/javascript/api/office-scripts/excelscript/excelscript.textconditionalformat) formats cells based on their text content. The formatting is applied when the text begins with, contains, ends with, or doesn't contain the given substring. |
| 193 | + |
| 194 | +The following sample marks any cell in the range that contains the text "review". |
| 195 | + |
| 196 | +:::image type="content" source="../../images/conditional-formatting-sample-text-comparison.png" alt-text="A table with status entries where any cell containing the word 'review' has a red fill and bold font."::: |
| 197 | + |
| 198 | +```typescript |
| 199 | +function main(workbook: ExcelScript.Workbook) { |
| 200 | + // Get the range to format. |
| 201 | + const sheet = workbook.getWorksheet("TextComparison"); |
| 202 | + const dataRange = sheet.getRange("B2:B6"); |
| 203 | + sheet.activate(); |
| 204 | + |
| 205 | + // Add conditional formatting based on the text in the cells. |
| 206 | + const textConditionFormat = dataRange.addConditionalFormat( |
| 207 | + ExcelScript.ConditionalFormatType.containsText).getTextComparison(); |
| 208 | + |
| 209 | + // Set the conditional format to provide a light red fill and make the font bold. |
| 210 | + textConditionFormat.getFormat().getFill().setColor("#F8696B"); |
| 211 | + textConditionFormat.getFormat().getFont().setBold(true); |
| 212 | + |
| 213 | + // Apply the condition rule that the text contains with "review". |
| 214 | + const textRule: ExcelScript.ConditionalTextComparisonRule = { |
| 215 | + operator: ExcelScript.ConditionalTextOperator.contains, |
| 216 | + text: "review" |
| 217 | + }; |
| 218 | + textConditionFormat.setRule(textRule); |
| 219 | +} |
| 220 | +``` |
| 221 | + |
| 222 | +## Top/bottom |
| 223 | + |
| 224 | +[Top/bottom conditional formatting](/javascript/api/office-scripts/excelscript/excelscript.topbottomconditionalformat) marks the highest or lowest values in a range. The highs and lows are based on either raw values or percentages. |
| 225 | + |
| 226 | +The following sample applies conditional formatting to show the two highest numbers in the range. |
| 227 | + |
| 228 | +:::image type="content" source="../../images/conditional-formatting-sample-top-bottom.png" alt-text="A sales table that has the top two values highlighted with a green fill and a bold font."::: |
| 229 | + |
| 230 | +```typescript |
| 231 | +function main(workbook: ExcelScript.Workbook) { |
| 232 | + // Get the range to format. |
| 233 | + const sheet = workbook.getWorksheet("TopBottom"); |
| 234 | + const dataRange = sheet.getRange("B2:D5"); |
| 235 | + sheet.activate(); |
| 236 | + |
| 237 | + // Set the fill color to green and the font to bold for the top 2 values in the range. |
| 238 | + const topBottomFormat = dataRange.addConditionalFormat(ExcelScript.ConditionalFormatType.topBottom).getTopBottom(); |
| 239 | + topBottomFormat.getFormat().getFill().setColor("green"); |
| 240 | + topBottomFormat.getFormat().getFont().setBold(true); |
| 241 | + topBottomFormat.setRule({ |
| 242 | + rank: 2, /* The numeric threshold. */ |
| 243 | + type: ExcelScript.ConditionalTopBottomCriterionType.topItems /* The type of the top/bottom condition. */ |
| 244 | + }); |
| 245 | +} |
| 246 | +``` |
| 247 | + |
| 248 | +## Custom conditions |
| 249 | + |
| 250 | +[Custom conditional formatting](/javascript/api/office-scripts/excelscript/excelscript.customconditionalformat) allows for complex formulas to define when formatting is applied. Use this when the other options aren't enough. |
| 251 | + |
| 252 | +The following sample sets a custom conditional formatting on the selected range. A light-green fill and bold font are applied to a cell if the value is larger than the value in the row's previous column. |
| 253 | + |
| 254 | +:::image type="content" source="../../images/conditional-formatting-sample-custom.png" alt-text="A row of a sales table. Values that are higher than the one to the left have a green fill and a bold font."::: |
| 255 | + |
| 256 | +```typescript |
| 257 | +function main(workbook: ExcelScript.Workbook) { |
| 258 | + // Get the range to format. |
| 259 | + const sheet = workbook.getWorksheet("Custom"); |
| 260 | + const dataRange = sheet.getRange("B2:H2"); |
| 261 | + sheet.activate(); |
| 262 | + |
| 263 | + // Apply a rule for positive change from the previous column. |
| 264 | + const positiveChange = dataRange.addConditionalFormat(ExcelScript.ConditionalFormatType.custom).getCustom(); |
| 265 | + positiveChange.getFormat().getFill().setColor("lightgreen"); |
| 266 | + positiveChange.getFormat().getFont().setBold(true); |
| 267 | + positiveChange.getRule().setFormula( |
| 268 | + `=${dataRange.getCell(0, 0).getAddress()}>${dataRange.getOffsetRange(0, -1).getCell(0, 0).getAddress()}` |
| 269 | + ); |
| 270 | +} |
| 271 | +``` |
0 commit comments