OpenOffice writer - programmatically updating tables with cell formulas

若如初见. 提交于 2019-12-12 12:17:15

问题


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).

Example

| 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.

Bookmark are in the table like this.

| 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

回答1:


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...

  • query text fields supplier from XTextDocument instance
  • query refreshable instance from text fields enumeration
  • call refresh

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.

Code

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


来源:https://stackoverflow.com/questions/9803552/openoffice-writer-programmatically-updating-tables-with-cell-formulas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!