To extend OpenOffice’s capabilities, I’ve defined some Python macros in a file in the user script directory (~/Library/Application Support/OpenOffice.org/3/user/Scripts/python/, in my case). The macros are visible in the Python macro organizer. However, using the functions in a cell formula results in “#NAME?” (OO.org error 525).
Suppose I’ve defined the following function :
def pytype(val):
return str(type(val))
How can I call pytype in a cell formula (e.g. =PYTYPE("string"))?
Background
I’m importing some data from Authorize.net into a MySQL database for analysis. MySQL can’t parse the date & time format used by Authorize.net into a DATETIME or TIMESTAMP field, so I’m trying to massage the data into a format MySQL can handle before import. OpenOffice also doesn’t recognize the data as a date & time and, as far as I’ve been able to determine, OO.Org doesn’t have a generic date parsing function. Thus I’m extending OO.org’s capabilities.
There are other approaches to the larger issue. For example, I could also try to fix up the data in MySQL post-import using additional columns. In fact, this is what I did the first time; however, there’s now existing data in the table to contend with. Because of that, and because there are other tasks in the future I hope to accomplish by using macros in formulas, for now I’m mostly interested in calling Python macros in formulae.
On the old OO.org forums, (super)user Villeroy posted an illustration of how to call Python functions from OO.org Basic, which can then be used in formulae. The key is to use the
com.sun.star.script.provider.MasterScriptProviderFactoryservice as a bridge. Here is an adaptation of his solution, generalized to call arbitrary functions in arbitrary modules:This can then be used to create a OO.org Basic function callable in a formula. Using the example
pytype:Another potential implementation is to create a Python add-in. However, this is a much heavier option as it requires installing the OpenOffice SDK, and it isn’t obvious to me whether this approach would work for free functions or only works for classes.