0

edited: Hey, I'm trying to get an app that somebody else wrote to call the function each time the spreadsheet is edited. I've been looking at the google developers website https://developers.google.com/apps-script/understanding_triggers and have tried a number of different options for syntax, but none of them have worked so far. Here's the function as it stands (I haven't included any of the syntax options I tried, just the function itself named 'onEdit'). All I want this function to do is count the number of cells with a non-white background in the specified range and update the count displayed in the cell that uses this add-on script when the spreadsheet is edited. Incidentally, does anyone know whether changing the background color of a cell counts as editing the spreadsheet? Thank you.

function onEdit(range) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();



  var r = sheet.getRange(range);
  var i = 0;
  var colors  = r.getBackgroundColors();

  for(c in colors) {

    if(colors[c] != "white") {
      i++;
    }
  }

  return i;


}
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Manatee86
  • 1
  • 3
  • What exactly is working and not working? – jfriend00 Jun 06 '14 at 00:26
  • The script works when run, but it doesn't update the value displayed in the cell I'm using the script when the number of cells with a non-white background color within the specified range changes. Editing the code in the script seems to make the number refresh, but that only seems to work sometimes and I want it to work automatically. Thanks. – Manatee86 Jun 07 '14 at 02:10

1 Answers1

0

SetInverval is not a javascript function. Its a dom function and there is no dom o server-side scripts. Read about triggers like onChange. You can only call setInterval in gas if you make an htmlService for a webapp, and cal it from the client side. But that's not what you want.

Zig Mandel
  • 19,571
  • 5
  • 26
  • 36
  • Thanks. I've gotten a trigger to work before, but it just displayed the result of the script in an alert box rather than changing the value displayed in the cell i'm Susie the script in. Since a lot of other calculations are based on the value in that cell, I need it to show up there rather than in an alert box. Any ideas? – Manatee86 Jun 07 '14 at 02:07
  • OK, I've been fiddling around with triggers and I've edited the question to reflect what I'm working with. Earlier, I had been using the triggers from the drop-down menu in the script editor, but since those didn't work for this script I have been trying to write the triggers into the code. I am now trying to call the function onEdit rather than at set intervals. Thanks. – Manatee86 Jun 09 '14 at 22:13
  • make a different question, since you are now asking something different, and you havent changed the code. – Zig Mandel Jun 10 '14 at 00:30
  • I have: http://stackoverflow.com/questions/24129861/correct-syntax-for-onedit-trigger I've also gotten this solution to work: http://stackoverflow.com/questions/9022984/google-apps-script-to-summarise-data-not-updating It's better than editing the script every time I want the values to update, but it still isn't automatic. – Manatee86 Jun 10 '14 at 16:25