vendredi 17 novembre 2017

Google sheets condition color format with script

I am trying to color format a row in a spreadsheet where I run a loop to check if any of the values is equal to 0 and if it is equal to 0 to color it red and if not color it red. I know I can do it with conditional format but I need this to activate after I press a button included in the sheet, so I need a script added to the button.

I have managed to write this script but it seems it color only the last cell from the range. Can you please tell me what am I doing wrong since I am new to coding.

function FormatColor() {

  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Acontobetaling');
  var range = sheet.getRange('D34:AA34');
  var values = [];
  var cell = [];

  for (var i = 1; i <= 24; i=i+1)
    cell = range.getCell(1, i)
    values = cell.getValue();
    if (values = '0') {
      cell.setBackground('red');
      } else {
      cell.setBackground('green');
      }
}

Aucun commentaire:

Enregistrer un commentaire