Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count the cells with same color in google spreadsheet

Tags:

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;    }  }   
like image 509
user1974764 Avatar asked Jan 16 '13 18:01

user1974764


People also ask

How do I count the same cell color in Google Sheets?

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.

Can I use Countif to count colored cells in Google Sheets?

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.

Can I use Countif to count colored cells?

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.


1 Answers

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!

  1. Install Power Tools through the Add-ons panel (Add-ons -> Get add-ons)
  2. From the Power Tools sidebar click on the Σ button and within that menu click on the "Sum by Color" menu item
  3. Select the "Pattern cell" with the color markup you want to search for
  4. Select the "Source range" for the cells you want to count
  5. Use function should be set to "COUNTA"
  6. Press "Insert function" and you're done :)
like image 75
Wolph Avatar answered Oct 19 '22 00:10

Wolph