Conditional formatting rules automatically change a cell's appearance based on its value. Apps Script lets you create these rules programmatically using SpreadsheetApp.newConditionalFormatRule() — applying them in bulk to an entire sheet, updating them when conditions change, or removing all rules at once.
Highlighting Closed Deals
functionhighlightClosedDeals(){var sheet =SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sales Tracker");var range = sheet.getRange("A2:H100");var rule =SpreadsheetApp.newConditionalFormatRule().whenFormulaSatisfied('=$G2="Closed"').setBackground("#c6efce").setFontColor("#276221").setRanges([range]).build();var rules = sheet.getConditionalFormatRules(); rules.push(rule); sheet.setConditionalFormatRules(rules);Logger.log("Conditional format rule applied for Closed.");}
Applying Multiple Status Rules at Once
This replaces all existing conditional format rules with a clean set for all three statuses:
functionapplyAllStatusRules(){var sheet =SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sales Tracker");var range = sheet.getRange("A2:H100");var rules =[SpreadsheetApp.newConditionalFormatRule().whenFormulaSatisfied('=$G2="Closed"').setBackground("#c6efce").setFontColor("#276221").setRanges([range]).build(),SpreadsheetApp.newConditionalFormatRule().whenFormulaSatisfied('=$G2="In Progress"').setBackground("#ffeb9c").setFontColor("#7d6608").setRanges([range]).build(),SpreadsheetApp.newConditionalFormatRule().whenFormulaSatisfied('=$G2="Pending"').setBackground("#fce4d6").setFontColor("#922b21").setRanges([range]).build()]; sheet.setConditionalFormatRules(rules);Logger.log("All status conditional format rules applied.");}
With the sample data, Sarah Johnson and Lisa Park's rows turn green, and Mark Chen's row turns yellow automatically.
Highlighting High-Value Deals
Flag Enterprise Plan deals (Amount > 500) in the Amount column:
Mark Chen's $999 Enterprise Plan amount cell is highlighted.
Highlighting Overdue Deals (Date > 30 Days Ago)
functionhighlightOverdueDeals(){var sheet =SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sales Tracker");var range = sheet.getRange("A2:H100");// Flag In Progress deals where the date is older than 30 daysvar rule =SpreadsheetApp.newConditionalFormatRule().whenFormulaSatisfied('=AND($G2="In Progress", $H2<TODAY()-30)').setBackground("#ffc7ce").setFontColor("#9c0006").setRanges([range]).build();var rules = sheet.getConditionalFormatRules(); rules.push(rule); sheet.setConditionalFormatRules(rules);Logger.log("Overdue deal highlight applied.");}
Removing All Conditional Format Rules
functionclearAllConditionalFormatRules(){var sheet =SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sales Tracker"); sheet.setConditionalFormatRules([]);Logger.log("All conditional format rules cleared.");}