OpenOffice writer-使用单元格公式以编程方式更新表 [英] OpenOffice writer - programmatically updating tables with cell formulas

查看:122
本文介绍了OpenOffice writer-使用单元格公式以编程方式更新表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当单元格值是书签并且书签以编程方式更新时(通过Java中的UNO调用),我真的很想尝试找出如何强制以编程方式刷新openoffice writer(3.3)单元格计算.

I'm really stuck trying to find out how to force a programmatic refresh of openoffice writer (3.3) cell calculations when the cell values are bookmarks and the bookmarks are updated programmatically (via UNO calls in Java).

示例

| start | stop  | duration    |
| 9:30  | 11:30 | = <A2>-<B2> | <= cell formula

当用户手动编辑表时,这很好用;当他们移动到下一个单元格时,值将更新.但是,如果我通过将文本插入单元格中的书签中以编程方式更新值,则所计算的单元格不会更新.如果您单击表,它们将更新,但是我希望这是自动的.

This works fine when the user is manually editing the table, the value is updated when they move to the next cell. However if I update the values programmatically by inserting text into bookmarks in the cells the calculated cells do not update. They will update if you click in the table but I would like this to be automatic.

书签在这样的表中.

| start     | stop    | duration    |
| <start0>  | <stop0> | = <A2>-<B2> |

用于更新书签的示例代码:

Example code to update the bookmark:

XBookmarksSupplier xBookmarksSupplier = (XBookmarksSupplier) UnoRuntime.queryInterface(XBookmarksSupplier.class, document); 
XNameAccess xNamedBookmarks = xBookmarksSupplier.getBookmarks(); 

Object bookmark = xNamedBookmarks.getByName("stop0"); 
XTextContent xBookmarkContent = (XTextContent) UnoRuntime.queryInterface(XTextContent.class, bookmark); 
xBookmarkContent.getAnchor().setString("11:30"); 

// code needed to force calculation of duration cell

推荐答案

我自己已经解决了.关键是使用 XRefreshable 并且知道表格中的单元格公式算作文本字段"-因此使用传统的long绕(但有趣)的UNO元编程样式...

I've figured it out myself. The key is to use XRefreshable and know that cell formulas in tables count as "text fields" - so using the traditional long-winded (but fun) UNO meta-programming style...

  • 从XTextDocument实例查询文本字段供应商
  • 通过文本字段枚举查询可刷新实例
  • 呼叫刷新

更新:如果要更新的公式中的源单元格应用了格式,则此操作将无效,目标单元格的格式也无关紧要.

Update: This will not work if the source cells in the formula being updated have a format applied to them, the format of the destination cell does not matter.

代码

XTextFieldsSupplier tfSupplier = (XTextFieldsSupplier) 
        UnoRuntime.queryInterface(XTextFieldsSupplier.class, document); 
XRefreshable refreshable = (XRefreshable) 
        UnoRuntime.queryInterface(XRefreshable.class, tfSupplier.getTextFields()); 
refreshable.refresh(); 

这篇关于OpenOffice writer-使用单元格公式以编程方式更新表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆