I have written an OpenOffice / LibreOffice add-in that implements some custom Calc spreadsheet functions in Java. Some of these functions create Java objects that are then logically associated with the result of the function and thus also associated with the cell that the function is used in.
(Assuming for now that the function is being called from a cell formula and not from the Function Wizard or some other context).
OpenOffice does not tell custom functions which cell they are being called from, so it is not easy to figure out that association. If the parameters to the function are cell addresses then it is possible to look at those cells and find the cells that are dependent on them - this narrows down the possibilities.
It could be possible to include a unique key for the Java object in the result if it is a string, but that is ugly. I could then periodically scan all cells to make sure that the unique key is still present in one or more cells.
However, the function could also be nested within other functions in a formula, so there is no guarantee that the key will end up being visible in the resulting value of the cell.
I am looking for a clean way to detect that the logical reference to the object has gone away (because the instance of the function was called with different args, or the formula containing the function has been deleted or altered).
Edit: Returning an XVolatileResult looked promising, however the removeResultListener() call-back is never called when a formula is deleted (except at document close).
Some more clarification: A custom function is implemented as an instance method of an Uno component (there may be more than one custom function on a single component). Only one instance of the component is ever created.
The function method is called with string or numeric arguments, corresponding to the arguments in the spreadsheet formula. It is also possible to receive the args as cell references rather than values.
The result of the function is a string or numeric value or an XVolatileResult. It is not possible to return an arbitrary Java object.
I am creating Java objects as a side-effect of evaluating the function and I want to be able to reference those objects in other formulas in other cells.