0
votes

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;


}
1

1 Answers

0
votes

onEdit is an event handler. you wont be able to return something from it. just initialise i outside, i.e., to the global scope.

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



      var r = sheet.getRange(range);

      var colors  = r.getBackgroundColors();

      for(c in colors) {

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


    }