How to check background color of a cell with a function?

Hi,

How would I check the background of a cell using a function? I tried recording a macro, hoping that it would give men an idea of what to do, but not quite.

What I would like to do is enter something like this in a cell:

=CellBGColor(A1)

For example, and get a value. I would like the function to work as a normal function when I cut and paste it, but I couldn’t even figure out how to pass a cell as a parameter to a function. So Either my level is not up to par with the rquirments of BASIC programming in LO, or I am misunderstanding something.

Thanks,

L

Answered here and here.

Thank you. I had seen the first one but not the second one. Neither of them gave me exactly wat I wanted but mixing both allowed me to get close.

L

@ Jim K: Since the linked answers are well-hung and things may change a bit with time, I would consider to give a reworked solution.
If you agree, you may reopen the question - or tell me to do so.

@Lupp: Okay, I reopened it. @lduperval, you can also post a solution if you have one.

I wanted to point out that I now prefer to pass a cell or a range (as references) to a user function based on VBA support. This is much simpler than the ways it can be done in “pure LibO API”.
In addition it is simple based on passed ranges to write the function equally capable of handling single cells and ranges needing array-output.
I also wanted to give a choice between the three main representations of the 24-bit-colors used by LibO, which are HEX (as a string), RGB (as a string), and the number of type Long simply return to cCalc as aresult of type ‘Number’.

Another remark: Any function for cell introspection like this one should not be misused on a permanent basis to work with sheets wrongly keeping data coded by attributes instead of plain data types. If someone has such a sheet functions reading attributes should exclusively be used to declutter them.

Suggested code:

Option VBAsupport 1 REM Helps to easier get access to referenced cell ranges.

Function cellBackgroundColor(pVBArange, Optional pCtrl As String, Optional pDummy)
REM pDummy can be used to trigger recalculations. (Volatile or otherwise. AutoCalc won't work.)
Dim res()
cellBackgroundColor = res
On Local Error Goto fail
If IsMissing(pCtrl) Then pCtrl="RGB"
If (pCtrl=0) OR (pCtrl="") Then pCtrl="RGB"
cellRg = pVBArange.CellRange
uC = cellRg.Columns.Count - 1
uR = cellRg.Rows.Count - 1
Redim res(uR, uC)
fa = createUnoService("com.sun.star.sheet.FunctionAccess")
For c = 0 To uC
  For r = 0 To uR
    j_BC = cellRg.getCellByPosition(c, r).CellBackColor
    Select Case Ucase(pCtrl)
      Case "D"
        res(r, c) = j_BC
      Case "RGB"
        res(r, c) = "(" & Red(j_BC) & ";" & Green(j_BC) & ";" & Blue(j_BC) & ")"
      Case "H"
        res(r, c) = "0x" & fa.callFunction("DEC2HEX", Array(j_BC, 6))
    End Select
  Next r
Next c
cellBackgroundColor = res
fail:
End Function

Example file: ask242690getCellBackColor_1.ods

To be clear: Next to everything in the code either is ordinary LibreOffice Basic or LibreOffice API usage with one exception: A function call in Calc placing a cell or a range reference on a parameter position for which the function didn’t specify a simple type, can be evaluated in the body of the function not only for the data buit also for the referred range. This can be very helpful though it feels somehow unsystematic.
Anyway the VBAsupport seems to be reliably insofar now. Do not use the feature in AOO (4.1.7 at present). There the VBA support is still insufficient as far as I can tell from my experience.

This is what I was looking for, the ability to put a cell name instead of a function result or a string. I will tweak this a bit for my purposes but it fits my needs. I think the VBASupport is what was missing from the previous solutions.

Thanks!

@Lupp: Thank you very much for your answer. I am a newbie at LO and have downloaded and opened the example file in LO.
The version that I use is Version: 7.0.2.2 (x64) on Windows 10.0 Build 19041;
The CELLBACKGROUNDCOLOR() function works fine for decimal and hexadecimal formats but the RGB format returns #VALUE! for all instances. Do I need to load or enable anything else in order to get the Red(j_BC), Green(j_BC) and Blue(j_BC) functions to work?

Very strange to me. Ijust checked the above attached example once more with LibreOffice V7.0.1.2, and everything worked flawlessly. I wouldn’t actually expect a bug in V7.0.2 to the reported effect. The ordinarty numeric (24-bit) color value is read from the cells in exactly the same way independent of the chosen output format. The only line of code being specific to the RGB case is
res(r, c) = "(" & Red(j_BC) & ";" & Green(j_BC) & ";" & Blue(j_BC) & ")", and this line uses the same variable j_BC as is used in the other cases. Ergo: If a bug, then it must occur with (at least) one of the Basic functions RED() or GREEN() or BLUE().
This only holds true, of course, if you didn’t change anything - and you user profile isn’t corrupted.

Hello. Can you support me, I have a big file with 20000 rows. I copied your code and run and had an error "BASIC runtime error ‘449’ Argument is not optional in line cellRg= pVBArange.cellRange
Thanks

Yes, it’s a common mistake to try to run a function that requires parameters. Do not “run”, but place the call in a table cell as a formula:
image