Skip to content

Instantly share code, notes, and snippets.

@chipoglesby
Last active May 18, 2022 23:34
Conditional Formatting in Google App Script based on cell value
//If the current cell is more than the previous cell, set it as lime green
function onEdit(e) {
var ss = SpreadsheetApp.getActive();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var name = sheet.getName();
var range = sheet.getRange("A:J");
var values = range.getValues();
var cell = sheet.getActiveCell();
var value = cell.getValue();
var currentRow = cell.getRow();
var currentColumn = cell.getColumn();
var previousPosition = currentColumn -1;
var previousPositionValue = sheet.getRange(currentRow, previousPosition).getValue();
//Loop through the range and if the active cell is greater than the previous cell, set to lime green
for(i in range) {
if(value > previousPositionValue){
cell.setBackground('#00ff00');
} else {
cell.setBackground('white');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment