Simple Example of UDF with Python ? (Calc)

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
SunStarBeansWhat
Posts: 4
Joined: Fri Sep 08, 2017 6:30 pm

Simple Example of UDF with Python ? (Calc)

Post 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!
LibreOffice 5.4 on Windows 7
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

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

Post by RoryOF »

I'm certainly not an expert with Python, but I think this might help you get started
http://christopher5106.github.io/office ... acros.html
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

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

Post by Villeroy »

With Python you can write extension packages with spreadsheet functions. Example: viewtopic.php?f=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: viewtopic.php?f=45&t=73145&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.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
SunStarBeansWhat
Posts: 4
Joined: Fri Sep 08, 2017 6:30 pm

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

Post 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!
LibreOffice 5.4 on Windows 7
Post Reply