I am using Google Spreadsheets to create a matrix (time/activity) as a daily schedule for staff to sign into.
Is there a formula to calculate the number of cells in a column that are shaded (or not shaded) a certain color?
For example: 20 of the cells are gray and 31 are not gray (shaded any other color) and instead of manually counting them, it calculates this at the bottom of reach column.
Best Answer
I have found a way to do this, using the scripting functionality that Google Spreadsheet provides.
Here's how to do it:
Code:
After following the above steps, you have three more functions available in the spreadsheet:
countCellsWithBackgroundColor(<color>, <range specification>)
sumWhereBackgroundColorIs(<color>, <range specification>)
getBackgroundColor(<cell specification>)
Please note that
<range specification>
and<cell specification>
are expressed in A1 notation, and must be enclosed in quotes.For example, to get the count of all the cells in the range B2:F13 that have the background color set to white, you should enter the following formula:
and to compute the sum of the same cells, use the formula:
Some cells may not have the background set to a color such as 'white', 'gray', but a RGB color like
#6fa8dc
. You cannot guess what the color is, so if you want to find out the color for a cell (for example,B9
), you should enter this formula in a cell:and afterwards use this value as a parameter to the two functions above.
It appears that if a cell's background color is a "theme color,"
getBackground()
incorrectly always returns#ffffff
. I haven't found a way around this other than to use a color from the standard set.Again, note the quotes in all above examples; without them, the formulas would not work.