Search code examples
javascriptgoogle-apps-scriptgoogle-sheetsgoogle-docsgoogle-apps

Google Sheet custom function returns 0


I'm following this thread to count all the cells with a particular color.

in my case, green/lime/#00ff00/rgb(0,255,0):

I've created my own sheet and tried to recreate these custom function without success.

The issue is that regardless of how I try to manipulate the function or cells, the function always returns 0 instead of the expected count of 14.

On its own getColor() works, but something seems to break down at the ArrayFormulat(SUM()) stage.


Solution

  • Try passing in the range as a string.. something like:

    =ArrayFormula(sum(--(getColor("C2:C15")=rgb(0,255,0))))
    

    If you want to use your formula, try:

    =sum(ArrayFormula(N(getColor( ADDRESS( ROW(C2),COLUMN(C2) )&":"&ADDRESS( ROW(C15), COLUMN(C15))) = rgb(0,255,0) )))
    

    Of course, the shortest way would be:

    =countif(getColor("C2:C15"), "#00ff00")
    

    All are working in this copy of your spreadsheet