[Solved] Testing Cell Background Color

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Joel
Posts: 2
Joined: Wed Feb 24, 2010 2:39 am

[Solved] Testing Cell Background Color

Post by Joel »

I am looking for a way to test Calc cell background colors using a Macro.
I have been unable to find a reasonable document with lists of supported statements, syntax and examples. My Google searches find lots of examples of changing cell background colors, but nothing to determine what the current background color is, and use that value. I have also gone through both the Basic and the Programmer guide books with multiple searches without success. I saw lots of good stuff, but not what I need right now.

Also, is there a color index value you can use (like red = 9) instead of RGB?

For example, the code below sets cell background for A1 and A2 to yellow, A3 and A4 to red, and leaves A5 and A6 alone.

I would like to write code that changes yellow cells to red, red cells to “no color”, and “no color” cells to yellow. I think the default background color is Transparent?

My question is, what is the test cell background color statement and syntax, what does it return, and how can I use it below? How do I correct the code below?

Code: Select all

Sub Loop1
'

 Dim A As Integer
 Dim Mx As Integer
 Dim r As Integer
 Dim c As Integer
Rem    R is row    C is column    Mx is maximum row    A is a spare variable

  Dim oSheet as Object
  oSheet = ThisComponent.CurrentController.ActiveSheet

REM   A1 A2 Cells to yellow
cell = osheet.getcellbyposition(0,0)
cell.cellbackcolor = rgb (255,255,0)
cell = osheet.getcellbyposition(0,1)
cell.cellbackcolor = rgb (255,255,0)

REM  A3 A4 Cells to red
cell = osheet.getcellbyposition(0,2)
cell.cellbackcolor = rgb (255,0,0)
cell = osheet.getcellbyposition(0,3)
cell.cellbackcolor = rgb (255,0,0)

 Mx = 5
 r = 0
 c = 0
 While r <= Mx

Cell = oSheet.getCellByPosition(c, r)
   
if [TEST CELL COLOR HERE] = [YELLOW] then
cell.cellbackcolor = rgb(255,0,0)
     	
elseif [TEST CELL COLOR HERE] = [RED] then
         	REM  I think 255,255,255 is transparent?
cell.cellbackcolor = rgb(255,255,255)

elseif [TEST CELL COLOR HERE] = [TRANSPARENT] then
cell.cellbackcolor = rgb(255,255,0)

endif

r = r + 1
Wend

End Sub
Last edited by Hagar Delest on Sat Feb 27, 2010 11:42 am, edited 1 time in total.
Reason: tagged [Solved].
User avatar
Villeroy
Volunteer
Posts: 31349
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Testing Cell Background Color

Post by Villeroy »

Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
CRL7
Posts: 8
Joined: Mon Dec 14, 2009 12:48 pm

Re: Testing Cell Background Color

Post by CRL7 »

Set background color of cell A1 of Sheet1 to for example yellow, A2 to red, A3 to whatever, etc, and run this:

Code: Select all

Sub Main
Dim Doc, MYSheet, MYCell As Object
Doc = thisComponent 
MYSheet = Doc.Sheets (0)
for rw=0 to 4
  MYCell= MYSheet.getCellByPosition(0,rw)
  MYSheet.getCellByPosition(1,rw).Value= MYCell.CellBackColor
next rw
End Sub
there you'll get values for colors, that you can use to set both background color or font color as:

Code: Select all

    If MYCell.CellBackColor=16776960 then
        MYCell.CellBackColor=16777215
    else...
OpenOffice 3.1 on Windows XP
Joel
Posts: 2
Joined: Wed Feb 24, 2010 2:39 am

Re: Testing Cell Background Color

Post by Joel »

Thanks very much. I am sure this will work and will try it tomorrow. Just what I was looking for.
Open Office Org 3.2.0
XP Home Serve Pack 2
Post Reply