如何解决如何在LibreOffice Calc中使用PyUNO更改单元格边框的线宽?
我正在编写一个Python脚本来自动调整LibreOffice Calc中的单元格边框。我想我知道我需要更改什么属性,但是当我为该属性分配新值时,该值不会更改。
例如,我编写了这段代码,将单个单元格的TopLine.linewidth从0更改为10。
package com.stackoverflow.questions;
import static org.apache.commons.io.FileUtils.forceDelete;
import static org.awaitility.Awaitility.await;
import static org.springframework.test.annotation.DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD;
import com.stackoverflow.questions.service.DirectoryManagerService;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBoottest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.integration.dsl.StandardIntegrationFlow;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.util.ReflectionTestUtils;
@SpringBoottest
public class MainFlowIntegrationTests {
private static final String MOCK_FILE_DIR = "intFiles/";
private static final String VALID_XML_MOCK_FILE = "valid01-student-01.xml";
private static final String INVALID_XML_MOCK_FILE = "invalid02-student-02.xml";
@Autowired
private MessageChannel fileReaderChannel;
@Autowired
private StandardIntegrationFlow mainStudentIntegrationFlow;
@Autowired
private DirectoryManagerService directoryManagerService;
private File queueDir;
private File processed;
private File error;
@BeforeEach
public void setup() throws IOException {
createrequiredDirectories();
moveFilesToQueueDir();
injectProperties();
mainStudentIntegrationFlow.start();
}
@AfterEach
public void tearDown() throws IOException {
mainStudentIntegrationFlow.stop();
deleterequiredDirectories();
}
@Test
public void readingFileAndMoveItToProcessedDir() throws IOException,InterruptedException {
// When: the fileReaderChannel receives a valid XML file
fileReaderChannel
.send(MessageBuilder.withPayload(new File(queueDir,VALID_XML_MOCK_FILE)).build());
// Then: the valid XML file should be sent to the processedDir
await().until(() -> processed.list().length == 1);
}
@Test
public void readingInvalidFileAndMoveItToErrorDir() throws IOException,InterruptedException {
// When: the fileReaderChannel receives a invalid XML file
fileReaderChannel
.send(MessageBuilder.withPayload(new File(queueDir,INVALID_XML_MOCK_FILE)).build());
// Then: the invalid XML file should be sent to the errorDir
await().until(() -> error.list().length == 1);
}
private void injectProperties() {
ReflectionTestUtils
.setField(directoryManagerService,"errorDir",error.getAbsolutePath().concat("/"));
ReflectionTestUtils
.setField(directoryManagerService,"processedDir",processed.getAbsolutePath().concat("/"));
}
private void copyFilesToQueueDir() throws IOException {
File intFiles = new ClassPathResource(MOCK_FILE_DIR).getFile();
for (String filename : intFiles.list()) {
FileUtils.copyFile(new File(intFiles,filename),new File(queueDir,filename));
}
}
private void createrequiredDirectories() throws IOException {
queueDir = Files.createTempDirectory("queueDir").toFile();
processed = Files.createTempDirectory("processedDir").toFile();
error = Files.createTempDirectory("errorDir").toFile();
}
private void deleterequiredDirectories() throws IOException {
forceDelete(queueDir);
forceDelete(processed);
forceDelete(error);
}
}
运行此代码后,我没有任何错误。并且我还确保我正在访问要修改的单元格。但是,此代码不会更改单元格的边框宽度。
我尝试通过在赋值前后打印值来进行一些调试:
# 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
有人知道我在做什么错吗?
解决方法
您需要将单元格属性设置为更改的边框对象。来自https://ask.libreoffice.org/en/question/145885/border-macro-no-longer-works/:
aThinBorder = oRange.TopBorder2
aThinBorder.LineWidth = 1
oRange.TopBorder2 = aThinBorder
,
因此,在进行了大量研究之后,我发现至少三种更改边框设置的方法。因为花了我很多力气,所以我认为我应该把它们留在这里,以便将来其他人可能更容易找到答案。
在所有示例中,我将单个单元格的TopBorder的LineWidth设置为10。
方法1:使用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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。