Skip to content

Instantly share code, notes, and snippets.

@martin0258
Created November 17, 2012 03:50
Show Gist options
  • Save martin0258/4093136 to your computer and use it in GitHub Desktop.
Save martin0258/4093136 to your computer and use it in GitHub Desktop.
Google spreadsheet conditional formatting based on other cell
/**
* A installable trigger function (not simple event handler).
* It is automatically invoked whenever the spreadsheet is edited.
* It will highlight the leftmost cell of each status cell if the value of the status cell is equal to 'N'.
*
* For more information about two event systems (1. installable 2. simple event handler), see
* http://productforums.google.com/forum/#!topic/apps-script/liSC7PE_V3g
*/
function onChange(e)
{
// You can change the following settings based on your preference.
var color = 'Red';
var unfinishedLabel = 'N';
var statusCellRange = 'C2:C10';
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange(statusCellRange);
var numRows = range.getNumRows();
var rowIndex = range.getRowIndex(); // top row index
var columnIndex = range.getColumnIndex(); // leftmost column index
for (var i = 0; i <= numRows - 1; i++)
{
var taskCell = sheet.getRange(rowIndex+i, 1);
var statusCell = sheet.getRange(rowIndex+i, columnIndex);
if ( statusCell.getValue() == unfinishedLabel ) { taskCell.setBackgroundColor(color); }
else { taskCell.setBackgroundColor('%COLOR%'); }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment