Calc/Add-In/Python How-To

From Apache OpenOffice Wiki
< Calc‎ | Add-In
Jump to: navigation, search

This example will show you how to create a Python add-in for OpenOffice.org Calc 3.x. The add-in from the fictive DoobieCompany will

  • Add custom spreadsheet functions to Calc.
  • Make the custom functions available in Calc's Function Wizard.
  • Be packaged as an .OXT file so an end-user can install the Add-in by simply double-clicking the .OXT package.

The interface definition is a text file that describes the functions that our Add-in will implement. Below is thedefinition of the DoobieDo interface that I have defined for this example. It demonstrates various data types and also how you can allow for optional parameters.

#include <com/sun/star/uno/XInterface.idl
 
module com { module doobiecompany { module examples { module DoobieDoo {
 
interface XDoobieDoo
{
long doobieMult( [in] long a, [in] long b );
double doobieDiv( [in] double a, [in] double b );
string doobieConcat( [in] string s1, [in] string s2 );
// The string 's3' should be optional. Model it as an 'any' 
// to allow it to be NULL/None/Missing.
string doobieConcatOptional( [in] string s1, [in] string s2, [in] any s3 ); 
};
 
}; }; }; };

The implementation of the DoobieDo interface in Python is shown below. This code can be found in the file src\doobiedo.py.

import uno
import unohelper
from com.doobiecompany.examples.DoobieDoo import XDoobieDoo
class DoobieDooImpl( unohelper.Base, XDoobieDoo ):
        def __init__( self, ctx ):
                self.ctx = ctx
        def doobieMult( self, a, b ):
                return a * b
        def doobieDiv( self, a, b ):
                return a / b
        def doobieConcat( self, s1, s2 ):
                return str(s1) + str(s2)
        def doobieConcatOptional( self, s1, s2, s3 ):
                if s3 == None:
                        return str(s1) + str(s2)
                else:
                        return str(s1) + str(s2) + str(s3)
 
def createInstance( ctx ):
        return DoobieDooImpl( ctx )
g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation( \
        createInstance,"com.doobiecompany.examples.DoobieDoo.python.DoobieDooImpl",
                ("com.sun.star.sheet.AddIn",),)

More detailed documentation is available in the full download here media:DoobieDoo-example.zip. It also includes some discussion about how to make user defined functions that work in both Calc and Excel.

The add-in was developed on Windows and the included make-script is Windows only. However, there is nothing particularly platform-specific code in the add-in, so I guess you should be able to make it work on a *nix platform too.

You may also visit the original URL to see if there is a more up-to-date version available.

Personal tools