Simple Example of UDF with Python ? (Calc)

Java, C++, C#, Delphi, ??? - Using the UNO bridges

Simple Example of UDF with Python ? (Calc)

Postby SunStarBeansWhat » Fri Sep 08, 2017 7:47 pm

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
SunStarBeansWhat
 
Posts: 4
Joined: Fri Sep 08, 2017 6:30 pm

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

Postby RoryOF » Fri Sep 08, 2017 10:47 pm

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
Apache OpenOffice 4.1.5 on Xubuntu 18.04 (mostly 64 bit version) and infrequently on Win2K/XP
14 October 2016 was Pooh's 90th birthday
User avatar
RoryOF
Moderator
 
Posts: 26716
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

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

Postby Villeroy » Fri Sep 08, 2017 11:10 pm

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.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 16.04, OpenOffice 4.x & LibreOffice 5.x
User avatar
Villeroy
Volunteer
 
Posts: 25381
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

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

Postby SunStarBeansWhat » Tue Sep 12, 2017 9:37 pm

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
SunStarBeansWhat
 
Posts: 4
Joined: Fri Sep 08, 2017 6:30 pm


Return to External Programs

Who is online

Users browsing this forum: No registered users and 1 guest