title: Count rows by background color failed
body: <p>Hey guys I am in google sheets excel and I want to count how many rows have the sheet based in background color.</p>
  
  <p>I have the next code:</p>
  
  <pre><code>function countColoredCells(countRange,colorRef) {
    var activeRg = SpreadsheetApp.getActiveRange();
    var activeSht = SpreadsheetApp.getActiveSheet();
    var activeformula = activeRg.getFormula();
    var countRangeAddress = activeformula.match(/\((.*)\,/).pop().trim();
    var backGrounds = activeSht.getRange(countRangeAddress).getBackgrounds();
    var colorRefAddress = activeformula.match(/\,(.*)\)/).pop().trim();
    var BackGround = activeSht.getRange(colorRefAddress).getBackground();
    var countCells = 0;
    for (var i = 0; i &lt; backGrounds.length; i++)
      for (var k = 0; k &lt; backGrounds[i].length; k++)
        if ( backGrounds[i][k] == BackGround )
          countCells = countCells + 1;
    return countCells;
  };
  
  function onOpen() {
    var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
    var entries = [{
      name : "Contar",
      functionName : "countColoredCells"
    }];
    spreadsheet.addMenu("Contar numero de casillas por color", entries);
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/Giue1.png" alt="Code"></p>
  
  <p>But when i put the next code doesn't work? Shows error. What's happens?</p>
  
  <pre><code>=countcoloredcells(B1:B9,A1)
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/EQT1N.png" alt="Error here"></p>
  
  <p><img src="https://i.stack.imgur.com/5oSSF.png" alt="Error log here"></p>
  
  <p><img src="https://i.stack.imgur.com/0qHCz.png" alt="Error with change of code suggested by Chris W"></p>
  
  <p><img src="https://i.stack.imgur.com/pbggn.png" alt="Error with change of code suggested by Chris W PART 2"></p>