Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing cell background color in LibreOffice

I am using LibreOffice 3.5.4.2. I would like to change the background color of cells based on various conditions. As a minimal example, I have the following macro/function defined:

function bgcolor()
Dim Doc As Object
Dim Sheet As Object
Dim Cell As Object   

Doc = ThisComponent
Sheet = Doc.Sheets(1)

Cell = Sheet.getCellByPosition(0, 0)
REM Cell.CellBackColor = RGB(50,60,70)
bgcolor=Cell.CellBackColor
end function

I execute the function by entering =BGCOLOR() into a cell. The cell in which that formula is present returns the color value of the first cell (0,0) or A1 on sheet 1, as expected.

However, I cannot get the function to change the background color of cell A1. The cell background color does not change when I remove the REM line in the example above to set the background color.

How can I set the background color of a cell with a function in LibreOffice?

(I read about using "styles", but did not look further at this because I need to set many different background colors and did not want to make many different styles. It is possible to manually change the background color without using styles, so I thought it would be possible to do the same programmatically.)

like image 813
SabreWolfy Avatar asked Aug 23 '12 09:08

SabreWolfy


People also ask

How do I change cell color in LibreOffice?

Click the icon Cell Styles (leftmost beneath the word Styles) Right click on Default and select New and then tab Organizer. Provide a name (e.g. BGgreen for a green background - but it is up to you) Change to tab Background , click button color and select your desired green color.

How do you add a background color to a cell in LibreOffice Calc?

Applying a Background Colour to a LibreOffice Calc Spreadsheet. Select the cells. Choose Format - Cells (or Format Cells from the context menu). On the Background tab page, select the background colour.

How do I change the background color in LibreOffice?

Under Tools → Options → LibreOffice → Application colors change the Document background colour. NOTE: If you don't like the predefined colours, it is possible to define new ones under Tools → Options → LibreOffice → Colors.

Which menu of LibreOffice calc the option to add background color to cells is set?

To apply background color to a cell or a group of cells, select and then click on the background color button on the Formatting toolbar. When you click on the arrow in this button you can select another color.


2 Answers

First, there is nothing wrong with your macro in general. The single line to set the CellBackColor is correct as stated. The problem is that the function is called from the sheet that you are attempting to modify. A function is not allowed to modify the sheet from which it is called. So, if you call your function from sheet 1 and then try to change the background color of a cell in sheet 1, that will fail. If, however, you attempted to change the background color of a cell on sheet 0 when calling from sheet 1, that will work as expected.

like image 61
Andrew Avatar answered Oct 07 '22 18:10

Andrew


the line should be

cell.cellbackcolor = RGB(50,60,70) 

(no "REM" there of course, that creates just commented line)

consider also the parameter of sheets to be 0 instead of 1 if you have only one sheet

for other interesting properties, see cell properties

like image 30
Micky Avatar answered Oct 07 '22 18:10

Micky