I'm trying count the number of cells with the same background color and put the result in other cell with a script in google apps script, but I can't do it. I have the next script but not work and I don't know which is the problem:
function countbackgrounds() {
var book = SpreadsheetApp.getActiveSpreadsheet();
var range_input = book.getRange("B3:B4");
var range_output = book.getRange("B6");
var cell_colors = range_input.getBackgroundColors()[0];
var color = "#58FA58";
var count = 0;
for( var i in cell_colors )
if( cell_colors[i] == color ){
range_output.setValue(++count);
}
else {
return count;
}
}
Easy solution if you don't want to code manually using Google Sheets Power Tools: