[英]Insert new row into LibreOffice writer table using UNO
I have recently tried to code a small java file which will insert a row into an already existing table in a .odt document. 我最近尝试编码一个小的Java文件,它将在.odt文档中的现有表中插入一行。 The table itself has 4 rows and 3 column, but I would like to implement a check which will expand that table if the content to be inserted is larger than 4. However, every time I try to get the table's rows, it returns a null pointer.
该表本身具有4行和3列,但是我想实现一个检查,如果要插入的内容大于4,它将对该表进行扩展。但是,每次我尝试获取表的行时,它都会返回null指针。 I am not that familiar with UNO api, but as far as i read through the documentation, the class XColumnsAndRowRange should be used in this situation.
我对UNO api不太熟悉,但是就我通读文档而言,在这种情况下应使用XColumnsAndRowRange类。 My code is as follows:
我的代码如下:
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);
}
I am not sure if I am missing something here or if I should be using a different function. 我不确定在这里是否缺少某些内容,或者是否应该使用其他功能。
As Lyrl suggested, this works: 正如Lyrl所建议的,这可以工作:
XTableRows rows = xTable.getRows();
Apparently XColumnRowRange is only used for spreadsheets. 显然XColumnRowRange仅用于电子表格。
Note: With Basic or Python you would not have this problem, because those languages do not need queryInterface
. 注意:使用Basic或Python,您将不会遇到此问题,因为这些语言不需要
queryInterface
。 The code would simply be: 该代码将是:
table = tables.getByName(tblName)
rows = table.getRows()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.