LibreOffice API/UNO:如何在编写器的表格单元格中水平右对齐文本

Posted

技术标签:

【中文标题】LibreOffice API/UNO:如何在编写器的表格单元格中水平右对齐文本【英文标题】:LibreOffice API/UNO: How to horizontal right-align text in table cell in writer 【发布时间】:2020-01-28 14:16:04 【问题描述】:

我正在使用 C++ 从另一个应用程序控制 LibreOffice/OpenOffice,但如果您也知道 java-bridge,我想您可以帮助我。所以基本上我想加载一个文档(作品),设置一个单元格的文本(作品)并将一个表格单元格设置为水平右对齐(我不知道该怎么做):

我愿意:

// Load Document
Reference <XInterface> rDoc = myLoader->loadComponentFromURL(...); 

// Get Table
Reference <XTextTablesSupplier> rTablesSuppl(rDocument, UNO_QUERY);
Any any = rTablesSuppl->getTextTables()->getByName("Table1");
Reference<XTextTable> rTable(any, UNO_QUERY);

// Set Text in cell
Reference<XCellRange> rRange (rTable, UNO_QUERY);
Reference<XCell> rCell = rRange->getCellByPosition(x, y);
Reference<XTextRange> rTextRange(rCell, UNO_QUERY);
rTextRange->setString("MyNewText");

// Align "MyNewText" right
????

知道如何继续吗?

【问题讨论】:

【参考方案1】:

警告...虽然我有使用 C++ 的经验,但我使用 Java 进行 LO API 编程,因此以下内容可能有点偏离。您可能需要稍作调整才能让事情顺利进行。

在 Java 中并使用单元格名称来获取单元格,我将单元格中的文本右对齐,如下所示:

XCell xCell = xTextTable.getCellByName(cellname);
XText xText = UnoRuntime.queryInterface(XText.class, xCell);
XPropertySet xPropertySet = UnoRuntime.queryInterface(XPropertySet.class, xText.getStart());
xPropertySet.setPropertyValue("ParaAdjust", com.sun.star.style.ParagraphAdjust.RIGHT);

在 C++ 中并使用单元格位置来获取单元格,我认为粗略的翻译是:

Reference<XCell> rCell = rRange->getCellByPosition(x, y);
Reference<XText> rText(rCell, UNO_QUERY);
Reference< XPropertySet > xPropSet( rText->getStart(), UNO_QUERY );
xPropSet->getPropertyValue("ParaAdjust") >>= com::sun::star::style::ParagraphAdjust.RIGHT;

鉴于您已经拥有的东西,您似乎可以将您的 ???? 替换为以下内容:

Reference< XPropertySet > xPropSet( rTextRange, UNO_QUERY );
xPropSet->getPropertyValue("ParaAdjust") >>= com::sun::star::style::ParagraphAdjust.RIGHT;

【讨论】:

以上是关于LibreOffice API/UNO:如何在编写器的表格单元格中水平右对齐文本的主要内容,如果未能解决你的问题,请参考以下文章

Libreoffice API (UNO):来自 xTextField 的文本和数据

Libreoffice API (UNO):需要更改用户的 xTextField 文本

如何在 libreoffice calc 中编写 python 宏来发送和接收数据

如何在 libreoffice writer 中编写 python 宏来发送接收数据

如何在 libreoffice calc 中编写 python 宏以在插入外部数据时处理合并的单元格

如何在 LibreOffice 中使用宏和 Visual Basic 突出显示文本