Skip to content

Instantly share code, notes, and snippets.

@sapegin
Created February 9, 2011 12:36
Show Gist options
  • Save sapegin/818396 to your computer and use it in GitHub Desktop.
Save sapegin/818396 to your computer and use it in GitHub Desktop.
Conditional rows formatting in Google Docs Spreadsheet
var styles = {
'default': {
color: '#000',
background: '#fff',
style: 'normal'
},
sent: {
color: '#ccc',
style: 'italic'
},
asked: {
color: '#974841',
background: '#EBCA9D'
},
answered: {
color: '#4F6228',
background: '#9BBB59'
}
};
function onEdit(event) {
var sheet = event.source.getActiveSheet();
if (sheet.getSheetName() != 'Обязательно')
return;
var rowIdx = event.source.getActiveRange().getRowIndex();
if (getCellValue(sheet, rowIdx, 'F') == 'Да')
appyStyle(sheet, rowIdx, 'answered');
else if (getCellValue(sheet, rowIdx, 'E') == 'Да')
appyStyle(sheet, rowIdx, 'asked');
else if (getCellValue(sheet, rowIdx, 'D'))
appyStyle(sheet, rowIdx, 'sent');
else
appyStyle(sheet, rowIdx, 'default');
}
function getCellValue(sheet, row, col) {
return sheet.getRange(col + row).getValue();
}
function appyStyle(sheet, row, name) {
var style = styles[name];
var defaults = styles['default'];
var range = sheet.getRange(row, 1, 1, sheet.getLastColumn());
range.setFontColor(style.color || defaults.color);
range.setBackgroundColor(style.background || defaults.background);
range.setFontStyle(style.style || defaults.style);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment