A rule turns cells green when they’re over target, and you want
to count the green ones. The problem: conditional formatting
is a display rule, not a stored fill. Google paints the
colour on screen but never writes it to the cell, so
getBackground() in Apps Script returns the cell’s
base colour — and every “count by colour”
tool, this add-on included, reads that same base colour. There is no
hidden API for the rendered colour.
Approach 1 — Count by the rule’s condition, not its colour
The colour is just a visual stand-in for a condition you already defined. Count that condition directly and you get a live number with no add-on at all.
Open Format → Conditional formatting, read the
exact test the rule uses, and translate it into
COUNTIF, COUNTIFS, SUMPRODUCT or
FILTER.
Approach 2 — Turn the highlight into a real fill, then count that
If you genuinely need to work from colour — the rules are complex, or colours were applied by different people over time — make the fill real:
- Select the range. Format → Conditional formatting, note each rule, then delete the rules.
- Re-apply the same colours as an actual fill colour (manually, or with a one-off Apps Script that reads each rule and calls
setBackground()). - Now the colour is stored in the cell, and counting cells by colour works normally.
Which approach should you use?
| By the condition | Convert to real fill | |
|---|---|---|
| Stays live as data changes | Yes | No — snapshot only |
| Needs an add-on | No | Only for the colour count itself |
| Handles many overlapping rules | Gets fiddly | Yes, once converted |
| Effort | Rewrite one condition | Strip rules + re-fill |
Almost always, count by the condition — it’s less work and it stays correct. Convert to a real fill only when the highlighting has drifted away from any single rule you can restate, or when the colour is really a manual label.
Counting real fill colours
Once your cells carry an actual background or font colour, Custom Count & Sum counts, sums and averages by it — one install, every spreadsheet on your account, nothing to script. Free for formulas up to 30 cells.
Install free from Google Workspace Marketplace