问题
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