|
| 1 | +--- |
| 2 | +title: Set conditional formatting for cross-column comparisons |
| 3 | +description: Learn how to apply conditional formatting and get input from the user. |
| 4 | +ms.date: 09/07/2023 |
| 5 | +ms.localizationpriority: medium |
| 6 | +--- |
| 7 | + |
| 8 | +# Set conditional formatting for cross-column comparisons |
| 9 | + |
| 10 | +This sample shows how to apply conditional formatting to a range. The conditions used are comparing values to those in an adjacent column. Additionally, this sample uses [parameters to get user input](../../develop/user-input.md). This lets the person running the script select the range, the type of comparison, and the colors. |
| 11 | + |
| 12 | +## Sample code: Set conditional formatting |
| 13 | + |
| 14 | +```TypeScript |
| 15 | +/** |
| 16 | + * Formats a range on the current sheet based on values in an adjacent column. |
| 17 | + * @param rangeAddress The A1-notation range to format. |
| 18 | + * @param compareTo The adjacent column to compare against. |
| 19 | + * @param colorIfGreater The color of the cell if the value is greater than the adjacent column. |
| 20 | + * @param colorIfEqual The color of the cell if the value is equal to the adjacent column. |
| 21 | + * @param colorIfLess The color of the cell if the value is less than the adjacent column. |
| 22 | + */ |
| 23 | +function main( |
| 24 | + workbook: ExcelScript.Workbook, |
| 25 | + rangeAddress: string, compareTo: "Left" | "Right", |
| 26 | + colorIfGreater: "Red" | "Green" | "Yellow" | "None", |
| 27 | + colorIfLess: "Red" | "Green" | "Yellow" | "None", |
| 28 | + colorIfEqual: "Red" | "Green" | "Yellow" | "None" |
| 29 | +) { |
| 30 | + // Get the specified range. |
| 31 | + const selectedSheet = workbook.getActiveWorksheet(); |
| 32 | + const range = selectedSheet.getRange(rangeAddress); |
| 33 | + |
| 34 | + // Remove old conditional formatting. |
| 35 | + range.clearAllConditionalFormats(); |
| 36 | + |
| 37 | + // Get the address of the first adjacent cell of the adjacent column. |
| 38 | + let adjacentColumn: string; |
| 39 | + if (compareTo == "Left") { |
| 40 | + adjacentColumn = range.getColumnsBefore().getCell(0, 0).getAddress(); |
| 41 | + } else { |
| 42 | + adjacentColumn = range.getColumnsAfter().getCell(0, 0).getAddress(); |
| 43 | + } |
| 44 | + |
| 45 | + // Remove the worksheet name from the address to create a relative formula. |
| 46 | + let formula = "=$" + adjacentColumn.substring(adjacentColumn.lastIndexOf("!") + 1); |
| 47 | + |
| 48 | + // Set the conditional formatting based on the user's color choices. |
| 49 | + setConditionalFormatting( |
| 50 | + range.addConditionalFormat(ExcelScript.ConditionalFormatType.cellValue).getCellValue(), |
| 51 | + colorIfGreater, |
| 52 | + formula, |
| 53 | + ExcelScript.ConditionalCellValueOperator.greaterThan); |
| 54 | + setConditionalFormatting( |
| 55 | + range.addConditionalFormat(ExcelScript.ConditionalFormatType.cellValue).getCellValue(), |
| 56 | + colorIfEqual, |
| 57 | + formula, |
| 58 | + ExcelScript.ConditionalCellValueOperator.equalTo); |
| 59 | + setConditionalFormatting( |
| 60 | + range.addConditionalFormat(ExcelScript.ConditionalFormatType.cellValue).getCellValue(), |
| 61 | + colorIfLess, |
| 62 | + formula, |
| 63 | + ExcelScript.ConditionalCellValueOperator.lessThan); |
| 64 | +} |
| 65 | + |
| 66 | +function setConditionalFormatting( |
| 67 | + conditionalFormat: ExcelScript.CellValueConditionalFormat, |
| 68 | + color: "Red" | "Green" | "Yellow" | "None", |
| 69 | + formula: string, |
| 70 | + operator: ExcelScript.ConditionalCellValueOperator |
| 71 | +) { |
| 72 | + // Pick the fill and font colors based on the preset color choices. |
| 73 | + if (color == "Red") { |
| 74 | + conditionalFormat.getFormat().getFont().setColor("#9C0006"); |
| 75 | + conditionalFormat.getFormat().getFill().setColor("#FFC7CE"); |
| 76 | + } else if (color == "Green") { |
| 77 | + conditionalFormat.getFormat().getFont().setColor("#001600"); |
| 78 | + conditionalFormat.getFormat().getFill().setColor("#C6EFCE"); |
| 79 | + } else if (color == "Yellow") { |
| 80 | + conditionalFormat.getFormat().getFont().setColor("#9C5700"); |
| 81 | + conditionalFormat.getFormat().getFill().setColor("#FFEB9C"); |
| 82 | + } else { /* None */ |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + // Apply the conditional formatting. |
| 87 | + conditionalFormat.setRule({ |
| 88 | + formula1: formula, |
| 89 | + operator: operator |
| 90 | + }); |
| 91 | +} |
| 92 | +``` |
0 commit comments