[英]How to change the LineWidth of a cell border using PyUNO in LibreOffice Calc?
I am writing a Python script to automatically adjust cell borders in LibreOffice Calc.我正在编写一个 Python 脚本来自动调整 LibreOffice Calc 中的单元格边框。 I think I know what property I need to change, however when I assign a new value to this property, the value does not change.
我想我知道我需要更改什么属性,但是当我为该属性分配一个新值时,该值不会改变。
For instance, I wrote this code to change the TopLine.LineWidth of a single Cell from 0 to 10.例如,我编写此代码将单个 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
I don't get any errors after running this code.运行此代码后,我没有收到任何错误。 And I have also made sure that I am accessing the cell I wish to modify.
而且我还确保我正在访问我希望修改的单元格。 However, this code does not change the cell's border width.
但是,此代码不会更改单元格的边框宽度。
I tried doing some debugging by printing the value before and after the assignment:我尝试通过在赋值前后打印值来进行一些调试:
# 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)
Does anyone know what I am doing wrong?有谁知道我做错了什么?
You need to set the cell property to a changed border object.您需要将单元格属性设置为更改的边框对象。 From https://ask.libreoffice.org/en/question/145885/border-macro-no-longer-works/ :
从https://ask.libreoffice.org/en/question/145885/border-macro-no-longer-works/ :
aThinBorder = oRange.TopBorder2
aThinBorder.LineWidth = 1
oRange.TopBorder2 = aThinBorder
So, after doing a lot of research, I found at least three methods to change border settings.所以,在做了大量的研究之后,我发现了至少三种改变边框设置的方法。 Because it took me so much effort, I figured I should leave them here so in the future other people may find the answer more easily.
因为花了我太多的努力,我想我应该把它们留在这里,以便将来其他人可以更容易地找到答案。
In all examples I'll set the LineWidth of the TopBorder of a single cell to 10.在所有示例中,我将单个单元格的 TopBorder 的 LineWidth 设置为 10。
Method 1: Using getPropertyValue() and setPropertyValue()方法 1:使用 getPropertyValue() 和 setPropertyValue()
cell = active_sheet.getCellByPosition(1, 1)
border_prop = cell.getPropertyValue("TopBorder")
border_prop.LineWidth = 10
cell.setPropertyValue("TopBorder", border_prop)
Method 2 (derived from Jim K's answer)方法 2(来自 Jim K 的回答)
cell = active_sheet.getCellByPosition(1, 1)
border_prop = cell.TopBorder2
border_prop.LineWidth = 10
cell.TopBorder2 = border_prop
Method 3: Using a BorderLine2 struct方法 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.