2

我正在编写一个 Python 脚本来自动调整 LibreOffice Calc 中的单元格边框。我想我知道我需要更改什么属性,但是当我为该属性分配一个新值时,该值不会改变。

例如,我编写了这段代码来将单个 Cell 的 TopLine.LineWidth 从 0 更改为 10。

# Access the current calc document
model = desktop.getCurrentComponent()
# Access the active sheet
active_sheet = model.CurrentController.ActiveSheet
# Get the cell and change the value of LineWidth
cell = active_sheet.getCellByPosition(2, 2)
cell.TableBorder2.TopLine.LineWidth = 10

运行此代码后,我没有收到任何错误。而且我还确保我正在访问我想要修改的单元格。但是,此代码不会更改单元格的边框宽度。

我尝试通过在赋值前后打印值来进行一些调试:

# This first print statement returns 0 because the cell has no borders
print(cell.TableBorder2.TopLine.LineWidth)
cell.TableBorder2.TopLine.LineWidth = 10
# This second print statement still returns 0, but I was expecting it to return 10
print(cell.TableBorder2.TopLine.LineWidth)

有谁知道我做错了什么?

4

2 回答 2

1

您需要将单元格属性设置为更改的边框对象。来自https://ask.libreoffice.org/en/question/145885/border-macro-no-longer-works/

aThinBorder = oRange.TopBorder2
aThinBorder.LineWidth = 1
oRange.TopBorder2 = aThinBorder
于 2020-09-19T17:17:04.663 回答
1

所以,经过大量研究,我发现了至少三种更改边框设置的方法。因为花了我这么多的努力,我想我应该把它们留在这里,以便将来其他人可以更容易地找到答案。

在所有示例中,我将单个单元格的 TopBorder 的 LineWidth 设置为 10。

方法一:使用 getPropertyValue() 和 setPropertyValue()

cell = active_sheet.getCellByPosition(1, 1)
border_prop = cell.getPropertyValue("TopBorder")
border_prop.LineWidth = 10
cell.setPropertyValue("TopBorder", border_prop)

方法 2(来自 Jim K 的回答)

cell = active_sheet.getCellByPosition(1, 1)
border_prop = cell.TopBorder2
border_prop.LineWidth = 10
cell.TopBorder2 = border_prop

方法 3:使用 BorderLine2 结构

border_prop = uno.createUnoStruct("com.sun.star.table.BorderLine2")
border_prop.LineWidth = 10
cell = active_sheet.getCellByPosition(1, 1)
cell.setPropertyValue("TopBorder", border_prop)
于 2020-09-20T20:58:51.250 回答