UDF/User defined function libreoffice basic with built-in help/tip

1.6k views Asked by At

I did a function in LibreOffice calc. It works, but I would like to have a conext help/tip capability, like the built in LibreOffice functions.

Example: when I type "=besselk(" it appaears a tip BESSELK(X; N). Using the function wizard a function and arguments description does also appear. Besselk besselk

Let's say I have a function to calculte a rectangle area

function arearect(a, b)
arearect = a * b
end function

I would like to have something like this:

function arearect(a, b)
FUNCTION DESCRIPTION "Compute rectangle area"
ARGUMENT DESCRIPTION "base length"
ARGUMENT DESCRIPTION "height"
arearect = a * b
end function

so, when I type "=arearect(" the argument description would appear and all descriptors would appear on Function Wizard.

thanks

1

There are 1 answers

4
Jim K On

Create the user-defined function as a Spreadsheet Add-In. Then enter the help text in the Description node in the .xcu file that defines the add-in.

For example, I created a function called REVERSE. Here is my CalcAddIns.xcu file:

<?xml version="1.0" encoding="UTF-8"?>
<oor:component-data xmlns:oor="http://openoffice.org/2001/registry"
          xmlns:xs="http://www.w3.org/2001/XMLSchema"
          oor:name="CalcAddIns" oor:package="org.openoffice.Office">
  <node oor:name="AddInInfo">
    <node oor:name="name.JimK.ReverseStringImpl" oor:op="replace">
      <node oor:name="AddInFunctions">
        <node oor:name="reverse" oor:op="replace">
          <prop oor:name="DisplayName"><value xml:lang="en">reverse</value></prop>
          <prop oor:name="Description">
            <value xml:lang="en">Flips a string backwards.  For example "apple" becomes "elppa".</value>
          </prop>
          <prop oor:name="Category"><value>Add-In</value></prop>
          <!-- This won't help, because there is no reverse() in Excel. -->
          <prop oor:name="CompatibilityName"><value xml:lang="en">reverse</value></prop>
          <node oor:name="Parameters">
            <node oor:name="s" oor:op="replace">
              <prop oor:name="DisplayName"><value xml:lang="en">s</value></prop>
              <prop oor:name="Description"><value xml:lang="en">The string to reverse.</value></prop>
            </node>
          </node>
        </node>
      </node>
    </node>
  </node>
</oor:component-data>

An Add-In also requires some other files. Here is my XCalcFunctions.idl:

#include <com/sun/star/uno/XInterface.idl>

module name { module JimK { module CalcFunctions {

    interface XCalcFunctions
    {
      string reverse( [in] string s );
    };

}; }; };

The actual implementation was simple. I used python:

def reverseString(inString):
    s = unicode(inString)
    # This is extended slice syntax [begin:end:step].  With a step of -1,
    # it will traverse the string elements in descending order.
    return s[::-1]

The result:

calc tooltip

EDIT:

There is another piece in Components.py for my extension:

class StringReverserAddIn(unohelper.Base, XCalcFunctions):
    def __init__(self, ctx):
        self.ctx = ctx

    @staticmethod
    def factory(ctx):
        return StringReverserAddIn(ctx)

    def reverse(self, inString):
        from lingt.app.calcfunctions import reverseString
        return reverseString(inString)

g_ImplementationHelper.addImplementation(
    StringReverserAddIn.factory,
    "name.JimK.LinguisticTools.ReverseStringImpl",
    ("com.sun.star.sheet.AddIn",),)

The file is declared in manifest.xml:

<!--- The Python code -->
<manifest:file-entry
 manifest:full-path="Components.py"
 manifest:media-type="application/vnd.sun.star.uno-component;type=Python"/>

The complete extension: https://extensions.libreoffice.org/extensions/lingtools.