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; } }
Count cell values based on cell color with script in Google sheet. 5. Then save this script code, and go back the sheet, enter this formula: =countcoloredcells(A1:E11,A1) into a blank cell, and then press Enter key to get the calculated result.
Use them to sum & count cells not only by their colors but also by the common contents. Ready-made SUMIFS & COUNTIFS formulas are included ;) If you work with colored cells in Google Sheets a lot, you may have probably tried our Function by Color add-on.
The short answer is that COUNTIF cannot be used to check for background color or any formatting; it can only test for values. If you only need to figure out the number of purple cells once or twice, you can use Excel's Find and Replace feature to figure it out.
Easy solution if you don't want to code manually using Google Sheets Power Tools:
Note: while the tool was free when this answer was written, it is now $43.20 per year or $89.95 for a lifetime license!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With