Page 1 of 1

Simple Example of UDF with Python ? (Calc)

PostPosted: Fri Sep 08, 2017 7:47 pm
by SunStarBeansWhat
Hello!

I am new to this OpenOffice/LibreOffice world. A long-time Excel user and programmer of SAS, SQL, VBA, Python, and more recently AWK! Learning AWK has completely sold me on doing things the Linux way, I truly enjoy it. I was trying to bring some of my custom Excel functions that I wrote in VBA over to LibreOffice 5's Calc, but found the LibreBasic syntax very unfamiliar; The internet seems to suggest that a User-Defined Function can be written in Python; does anyone have a simple example of a UDF they've successfully implemented in Calc? I think it could really help me get started. :fist:

Thanks!

Re: Simple Example of UDF with Python ? (Calc)

PostPosted: Fri Sep 08, 2017 10:47 pm
by RoryOF
I'm certainly not an expert with Python, but I think this might help you get started
http://christopher5106.github.io/office/2015/12/06/openoffice-libreoffice-automate-your-office-tasks-with-python-macros.html

Re: Simple Example of UDF with Python ? (Calc)

PostPosted: Fri Sep 08, 2017 11:10 pm
by Villeroy
With Python you can write extension packages with spreadsheet functions. Example: https://forum.openoffice.org/en/forum/v ... 20&t=83856
You have to implement service com.sun.star.sheet.AddIn and write some configuration foo. This is not simple but it is very efficient, localizable, fully integrated into the spreadsheet GUI and you can even integrate localized help files.

With Basic you can not implement any services, therfore you can not write "true extensions", only extensions that add macros. But with Basic macros you can call Python macros: https://forum.openoffice.org/en/forum/v ... 5&p=329658 and Basic provides the most simple way to call Basic functions from spreadsheets. Just make sure that the function name is unique and the code is stored in a "Standard" library which is always loaded by default.

Re: Simple Example of UDF with Python ? (Calc)

PostPosted: Tue Sep 12, 2017 9:37 pm
by SunStarBeansWhat
Thank you for the responses.. I need to issue a correction. In my original post I claimed that the BASIC language that came with LibreOffice seemed unfamiliar, but that was wrong; I based this claim on a recorded macro, which was a bad idea.
One should not base their understanding of LibreOffice Basic or OpenOffice Basic on a recorded macro. The syntax of the Language is actually suspiciously similar to VBA. This is a significant relief: a few of my UDFs have come over with no modifications already! Woo!