title: Why is GetActiveSheet causing this error?
body: <p>I'm trying to add a function to add the values of specific cells depending on their background color. I keep getting the same error though.</p>
  
  <p>N/A</p>
  
  <hr>
  
  <pre><code>function sumbycolor(sumRange,colorRef) {
    var activeRange = SpreadsheetApp.getActiveRange();
    var activeSheet = activeRange.getActiveSheet();
    var formula = activeRange.getFormula().toString();
    formula = formula.replace(new RegExp(';','g'),',');
  
  
    var rangeA1Notation = formula.match(/\((.*)\,/).pop();
    var range = activeSheet.getRange(rangeA1Notation);
    var bg = range.getBackgrounds();
    var values = range.getValues();
  
    var colorCellA1Notation = formula.match(/\,(.*)\)/).pop();
    var colorCell = activeSheet.getRange(colorCellA1Notation);
    var color = colorCell.getBackground();
  
    var total = 0;
  
    for(var i=0;i&lt;bg.length;i++)
      for(var j=0;j&lt;bg[0].length;j++)
        if( bg[i][j] == color )
          total=total+(values[i][j]*1);
     return total;
  };
  </code></pre>
  
  <hr>
  
  <p>var activeSheet = activeRange.getActiveSheet();</p>
  
  <p>That line is giving me the error "Cannot call method "GetActiveSheet" of null.</p>
  
  <p>I don't know how to fix this.</p>