Multiple column conditional formatting

允我心安 提交于 2019-12-07 23:06:56

问题


I have searched for an answer to this, but I cannot find one anywhere.

I am trying to set a background color of an individual cell based on the contents of that cell and another cell.

For example, I want A1 to have a background color of green if that specific cell is blank AND if B1 does not have an "x" in it. If either condition is not met, i.e. there is something in A1 or B1 does not have an "x", I don't want A1 to be highlighted.

I then want to apply this to the entire column A, and have it update each time columns A or B are edited.


回答1:


function onEdit() {
  var ss = SpreadsheetApp.getActiveSpreadsheet(),
      s = ss.getSheetByName('Sheet1'),
      values = s.getRange('A1:B').getValues(),
      row, len, colors = [];

  // CHECK VALUES FOR CONDITIONS
  for (row = 0, len = values.length; row < len; row++) {
    if (values[row][0] == '' && values[row][1] != 'x')
      colors.push(['green']);
    else colors.push(['white']);}

  // SET NEW COLORS ALL AT ONCE
  s.getRange('A1').offset(0, 0, len).setBackgrounds(colors);
}


来源:https://stackoverflow.com/questions/16658032/multiple-column-conditional-formatting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!