I'm trying to produce a script that changes cell colour depending on the contents of other cells in the row.
Basically, I'd like to change the cell (col 6) to red if the date (col 1) of the entry is further than two days prior AND the number that cell is less than 1. But if a third column (col 5) changes to Y, format the cell as green.
I'm having trouble with getRange on line 5 returning null, but wanted to check i was doing this correctly. Thanks!
function formatting() { var now = new Date().getTime(); var twoDaysInMilliseconds = 172800000; var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Enquiry Tracking'); var columnBooked = sheet.getRange(2, 5, sheet.getLastRow()-1, 1); var bValues = columnBooked.getValues(); for (var i = 0; i < bValues.length; i++) { var columnFU = sheet.getRange(i + 2, 6, 1, 1); if (bValues[i][0] != 'y') { var rowdate = new Date(sheet.getRange(i + 2, 1, 1, 1).getValue()).getTime(); if (now - rowdate > twoDaysInMilliseconds) AND (columnFU.getValue() < 1) { columnFU.setBackgroundColor('red'); } } else { columnFU.setBackgroundColor('green'); } } }