使用 UNO 将新行插入 LibreOffice writer table
Insert new row into LibreOffice writer table using UNO
我最近尝试编写一个小 java 文件,该文件将在 .odt 文档中的现有 table 中插入一行。 table 本身有 4 行和 3 列,但我想执行一个检查,如果要插入的内容大于 4,它将扩展 table。但是,每次我尝试获取table 的行,它 returns 是一个空指针。我不太熟悉 UNO api,但据我阅读文档,在这种情况下应该使用 class XColumnsAndRowRange。我的代码如下:
XTextTablesSupplier xTablesSupplier = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, xTextDocument);
XNameAccess xNamedTables = xTablesSupplier.getTextTables();
try {
Object table = xNamedTables.getByName(tblName);
XTextTable xTable = (XTextTable) UnoRuntime.queryInterface(XTextTable.class, table);
XCellRange xCellRange = (XCellRange) UnoRuntime.queryInterface(XCellRange.class, table);
if(flag){
XColumnRowRange xCollumnAndRowRange =(XColumnRowRange)
UnoRuntime.queryInterface(XColumnRowRange.class, xCellRange);
XTableRows rows = xCollumnAndRowRange.getRows();
System.out.println("Testing if this works");
rows.insertByIndex(4, size-4);
}
我不确定我是否遗漏了什么,或者我是否应该使用不同的功能。
正如 Lyrl 所建议的,这可行:
XTableRows rows = xTable.getRows();
显然 XColumnRowRange 仅用于电子表格。
注意:使用 Basic 或 Python 你不会有这个问题,因为这些语言不需要 queryInterface
。代码只是:
table = tables.getByName(tblName)
rows = table.getRows()
我最近尝试编写一个小 java 文件,该文件将在 .odt 文档中的现有 table 中插入一行。 table 本身有 4 行和 3 列,但我想执行一个检查,如果要插入的内容大于 4,它将扩展 table。但是,每次我尝试获取table 的行,它 returns 是一个空指针。我不太熟悉 UNO api,但据我阅读文档,在这种情况下应该使用 class XColumnsAndRowRange。我的代码如下:
XTextTablesSupplier xTablesSupplier = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, xTextDocument);
XNameAccess xNamedTables = xTablesSupplier.getTextTables();
try {
Object table = xNamedTables.getByName(tblName);
XTextTable xTable = (XTextTable) UnoRuntime.queryInterface(XTextTable.class, table);
XCellRange xCellRange = (XCellRange) UnoRuntime.queryInterface(XCellRange.class, table);
if(flag){
XColumnRowRange xCollumnAndRowRange =(XColumnRowRange)
UnoRuntime.queryInterface(XColumnRowRange.class, xCellRange);
XTableRows rows = xCollumnAndRowRange.getRows();
System.out.println("Testing if this works");
rows.insertByIndex(4, size-4);
}
我不确定我是否遗漏了什么,或者我是否应该使用不同的功能。
正如 Lyrl 所建议的,这可行:
XTableRows rows = xTable.getRows();
显然 XColumnRowRange 仅用于电子表格。
注意:使用 Basic 或 Python 你不会有这个问题,因为这些语言不需要 queryInterface
。代码只是:
table = tables.getByName(tblName)
rows = table.getRows()