Google Spreadsheet conditional formatting script

In the new Google sheets, this no longer requires a script.

Instead, in conditional formatting, select the option "custom formula", and put in a value like =O2="X" - or indeed any expression that returns a boolean true/false value.

From what I can tell, the references listed in these custom scripts are a bit weird, and are applied as follows...

If it's a cell within your selected range, then it is changed to "the cell that's being highlighted".

If it's a cell outside your selected range, then it's changed to "that position, plus an offset the same as the offset from the current cell to the top left of the selected range".

That is, if your range was A1:B2, then the above would be the same as setting individual formatting on each cell as follows:

A1 =O2="X"
A2 =O3="X"
B1 =P2="X"
B2 =P3="X"

You can also specify fixed references, like =$O$2="X" - which will check the specific cell O2 for all cells in your selected range.


Here's a script you could use to do what you described:

function formatting() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
  var columnO = sheet.getRange(2, 15, sheet.getLastRow()-1, 1);
  var oValues = columnO.getValues();

  for (var i = 0; i < oValues.length; i++) {
    if (oValues[i][0] == 'X') {
      sheet.getRange(i + 2, 1, 1, 1).setBackgroundColor('green');
    }
  }
}