OpenOfficeAdding functions to spreadsheets


Contents

Introduction
Download the example
Adding functions
Implementing functions
Building the example

Introduction

OpenOffice.org Calc is a program for editing spreadsheet documents. For numerous situations this program provides a variety of helpful functions. Nevertheless, particularly in the area of the spread-sheet analysis problems can occur, in which a situation-suitable extension of the function range appears desirable.

In the following one describes, where an example can be downloaded and how individual functions can be added and implemented to the Calc program. The possibility of an extension presented here was implemented in the programming language Java.

If you like to have more information on creating components with Java you should take a look at the web page Java in UNO and OpenOffice.

Download the example

The example described below is available for downloading in a packed version. With the help of the script files for the operating systems Unix and Windows all necessary steps can be carried out.

Adding functions

All methods, which should extend the program Calc and should provide a certain functionality, are collected in an interface named XCalcAddins.

In order to provide a language-independent programming environment, all interfaces must be described in an interface definition language (IDL). The interface XCalcAddins is described in the UNO interface definition language (UNOIDL):

#include <com/sun/star/uno/XInterface.idl>
#include <com/sun/star/beans/XPropertySet.idl>
module com {
  module sun {
    module star {
      module sheet {
        module addin {
          interface XCalcAddins : com::sun::star::uno::XInterface {
            long getMyFirstValue( [in] com::sun::star::beans::XPropertySet xOptions );
            long getMySecondValue( [in] com::sun::star::beans::XPropertySet xOptions,
                                   [in] long intDummy );
          };
          service CalcAddins {
            interface XCalcAddins; 
          };
        };
      };
    };
  };
};

You may declare several methods like getMyFirstValue or getMySecondValue. Each method may have any number of arguments after xOptions. Furthermore, you could replace the name of the service and the interface, but only if you want to replace this name in all your project files. This example will work with the default names. All interfaces specified in UNOIDL must be derived from com.sun.star.uno.XInterface .
Interface names should start with an X prefix.

Implementing functions

First the user should look at the file CalcAddins.java, in order to get a first overview of the program code in Java. The file is commentated in numerous places. All positions in the program, at which the user should become active, are already marked by the keyword "TO DO".

The file consists of an inner and an outer class.

public class CalcAddins {
  static public class _CalcAddins implements XCalcAddins,XAddIn,XServiceName,XServiceInfo,XTypeProvider {
    ...

  }
               
  public static XSingleServiceFactory __getServiceFactory(String implName,
      XMultiServiceFactory multiFactory,
      XRegistryKey regKey) {
    ...
  }
               
  public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
    ...
  }
}

The outer class CalcAddins provides

  • a method __getServiceFactory to instantiate the component on demand,
  • a method __writeRegistryServiceInfo to give information about the component and
  • an inner class.

The inner class _CalcAddins provides the component as a concrete implementation of the service description. It implements the needed interfaces:

All positions in the Java file marked with the key word "TO DO" should be edited by the user. For a better understanding they are explained in the following.

You should replace these method names by the method names of your interface.

private static final String[] stringFunctionName = {
"getMyFirstValue",
"getMySecondValue"
};

For each of your methods you should make up a new constant with a different value.

private static final short shortGETMYFIRSTVALUE = 0;
private static final short shortGETMYSECONDVALUE = 1;

This is where you implement all methods of your interface. The parameters have to be the same as in your IDL file and their types have to be the correct IDL-to-Java mappings of their types in the IDL file.

public int getMyFirstValue( com.sun.star.beans.XPropertySet xOptions ) {
return (int) 1;
}

public int getMySecondValue( com.sun.star.beans.XPropertySet xOptions, int intDummy ) {
return( (int) ( 2 + intDummy ) );
}

You should list all argument names for each of your methods, here.

switch( this.getFunctionID( stringProgrammaticFunctionName ) ) {
case shortGETMYFIRSTVALUE:
switch( intArgument ) {
case 0:
stringReturn = "(internal)";
break;
}
break;
case shortGETMYSECONDVALUE:
switch( intArgument ) {
case 0:
stringReturn = "(internal)";
break;
case 1:
stringReturn = "intDummy";
break;
}
break;
}

Assign the name of each of your methods.

switch( this.getFunctionID( stringProgrammaticName ) ) {
case shortGETMYFIRSTVALUE:
stringReturn = "getMyFirstValue";
break;
case shortGETMYSECONDVALUE:
stringReturn = "getMySecondValue";
break;
}

Enter a description for each of your methods that office users will understand.

switch( this.getFunctionID( stringProgrammaticName ) ) {
case shortGETMYFIRSTVALUE:
stringReturn = "This is your first method.";
break;
case shortGETMYSECONDVALUE:
stringReturn = "This is your second method.";
break;
}

Enter a description for every argument of every method. Make them so that office users will understand.

switch( this.getFunctionID( stringProgrammaticFunctionName ) ) {
case shortGETMYFIRSTVALUE:
switch( intArgument ) {
case 0:
stringReturn = "(internal)";
break;
}
break;
case shortGETMYSECONDVALUE:
switch( intArgument ) {
case 0:
stringReturn = "(internal)";
break;
case 1:
stringReturn = "You can add this value.";
break;
}
break;
}

Building the example

After the above steps were executed, still the script compileCalcAddins<YourOperatingSystem> must be launched. Before you execute the script you should adapt the environment variables to your needs.

For the execution of the script files the ODK is needed.

Closer information you can find on the web page How to write a UNO component in Java.

The script will produce a JAR file. In order to register this jar file you could use a StarBasic macro. You only have to create a new macro (menu Extras/Macro) and copy the following StarBasic script into this macro. Furthermore, you should replace the existing path of the JAR file with your JAR file. Finally you should excecute the new macro.

After a restart of your Office your Calc should provide all new functions (menu Insert/Functions).

 


Author: Bertram Nolte (Tue Jun 5 17:17:40 MEST 2001)
Copyright 2001 OpenOffice.org Foundation. All Rights Reserved