title: Count number of coloured cells in column with dynamic number of rows
body: <p>At the bottom of each column I'm trying to display how many cells in that column have a particular background colour. This is what I'd like it to do:
  <a href="http://postimg.org/image/ltn9ojtgb/" rel="nofollow">Picture of intended result</a></p>
  
  <p>The problem is it's a Gantt chart and I'll be inserting rows constantly. I can do it with a fixed number of rows using:</p>
  
  <pre><code>=countCellsWithBackgroundColor("#cfe2f3", "B1:B22")
  </code></pre>
  
  <p>which uses this function I found:</p>
  
  <pre><code>function countCellsWithBackgroundColor(color, rangeSpecification) {
  
    var sheet = SpreadsheetApp.getActiveSpreadsheet();
    var range = sheet.getRange(rangeSpecification);
  
    var x = 0;
  
    for (var i = 1; i &lt;= range.getNumRows(); i++) {
      for (var j = 1; j &lt;= range.getNumColumns(); j++) {
  
        var cell = range.getCell(i, j);
  
        if(cell.getBackgroundColor() == color)
          x++;
      }
    }
  
    return x;
  }
  </code></pre>
  
  <p>I just can't figure out how to give that function every cell in the column. I get errors like "must be a range".</p>