在LibreOffice中更改单元格背景颜色 [英] Changing cell background color in LibreOffice

查看:454
本文介绍了在LibreOffice中更改单元格背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用LibreOffice 3.5.4.2.我想根据各种条件更改单元格的背景颜色.作为最小的示例,我定义了以下宏/函数:

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

我通过在单元格中输入=BGCOLOR()来执行功能.出现该公式的单元格按预期返回工作表1上第一个单元格(0,0)A1的颜色值.

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.

但是,我无法获得更改单元格A1的背景颜色的功能.当我在上例中删除REM行以设置背景颜色时,单元格背景颜色不会不变.

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.

如何在LibreOffice中使用功能设置单元格的背景颜色?

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.)

推荐答案

首先,您的宏通常没有任何问题.如上所述,用于设置CellBackColor的单行是正确的.问题是,您正在尝试修改的工作表中调用了该函数.不允许某个函数修改从中调用该表的表单.因此,如果您从工作表1调用函数,然后尝试更改工作表1中单元格的背景颜色,那将失败.但是,如果您在尝试从工作表1调用时更改工作表0上的单元格的背景颜色,它将按预期工作.

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.

这篇关于在LibreOffice中更改单元格背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆