Variable Results

From Apache OpenOffice Wiki
Jump to: navigation, search

It is also possible to implement functions with results that change over time. Whenever such a result changes, the formulas that use the result are recalculated and the new values are shown in the spreadsheet. This can be used to display data from a real-time data feed in a spreadsheet.

In its interface, a function with a variable result must be defined with a return type of, such as the getCounter() function from the example interface above. The function's implementation must return an object that implements the service. Subsequent calls to the same function with the same arguments return the same object. An implementation that returns a different result object for every name looks like this:

  private java.util.Hashtable aResults = new java.util.Hashtable();
  public getCounter(String aName) {
      ExampleAddInResult aResult = (ExampleAddInResult) aResults.get(aName);
      if (aResult == null) {
          aResult = new ExampleAddInResult(aName);
          aResults.put(aName, aResult);
      return aResult;

The result object has to implement the addResultListener() and removeResultListener() methods from the interface to maintain a list of listeners, and notify each of these listeners by calling the interface's modified() method whenever a new result is available. The object that is passed to the modified() call must contain the new result in the Value member. The possible types for the result are the same as for a function's return value if no volatile results are involved.

If a result is already available when addResultListener() is called, it can be publicized by immediately calling modified() for the new listener. Otherwise, the spreadsheet application displays a "#N/A" error value until a result is available.

The following example shows a simple implementation of a result object. Every time the incrementValue method is called, for example, from a background thread, the result value is incremented and the listeners are notified.

  class ExampleAddInResult implements {
      private String aName;
      private int nValue;
      private java.util.Vector aListeners = new java.util.Vector();
      public ExampleAddInResult(String aNewName) {
          aName = aNewName;
      private getResult() {
 aEvent = new;
          aEvent.Value = aName + " " + String.valueOf(nValue);
          aEvent.Source = this;
          return aEvent;
      public void addResultListener( aListener) {
          // immediately notify of initial value
      public void removeResultListener( aListener) {
      public void incrementValue() {
 aEvent = getResult();
          java.util.Enumeration aEnum = aListeners.elements();
          while (aEnum.hasMoreElements())
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages