How to use Python custom functions in a calc table?

I have a table that looks like this:
QQ图片20230501100916
I wrote a custom function “cx” using a Python script.
When I call a Python custom function using BASIC, I see the following error:


How Do I make this Python function available like “=cx(“A1:E7”,0,“A”)” in a calc table?

1 Like

My tips:

  • You can write it as a Python Extension (Add-In type). Then the function will be usable az all of the other cell functions.

  • You can write a StarBasic Wrapper function for calling the Python function. It must be located in the Standard Library.

  • You can write the Cell function in StarBasic directly. It must be located in the Standard Library.

Python LibreOffice Programming Part 4: Calc of OooDev may have some hints for this.

https://wiki.openoffice.org/wiki/Add-In

Hmm. Its “Preface” tells:

Star Basic is the native language of Star Office which has been incorporated by OpenOffice and LibreOffice. This means that all other supported languages must negotiate a basic bridge.

What is this? This is simply wrong.

Much of this OooDev project is inspired by the work of Dr. Andrew Davison and his work on ‘Java LibreOffice Programming’. In his book this he makes this claim that “All other supported languages must negotiate a basic bridge”. From my work with OooDEv this seems to be correct.

As shown in Python LibreOffice Programming 2.1 Starting Office a bridge is indeed used to connect to LibreOffice. OooDev as Class LoSocketStart and Class LoPipeStart and for macros (which don’t need a bridge) Class LoDirectStart is used internally.

There are numerous example in the auto section of LibreOffice Python UNO Examples project such as the Build Table example.

I you have a correction to add to the documentation I would be pleased to look it over.

1 Like

“basic bridge” != “Basic bridge”

And even != BASIC bridge.

1 Like

The bridge referred there is the UNO bridge and UNO Language Binding.
See LibreOffice Developer's Guide: Chapter 2 - Professional UNO - The Document Foundation Wiki and LibreOffice Developer's Guide: Chapter 5 - Advanced UNO - The Document Foundation Wiki.

1 Like

“basic bridge” != “Basic bridge” != BASIC bridge

Ah yes, with this correction, it would be OK. Note that this reading of the original text is artificial, though; because the preface (dedicated to Python, based on a text dedicated to Java) explicitly (and unexpectedly) starts with some words about Basic language, claiming that that language is “native”, and continues with “This means that other …”, making the “basic” be equal to “Basic” (and “this means”, to BASIC :wink:).

A correct text would be something like

Scripting support for any programming language is implemented in LibreOffice using respective UNO bridges.

And Basic bridge is no different from Python bridge in this :slight_smile:
There is one place where Basic is “superior” to other languages (unrelated to the bridge): Calc allows to use its functions as User-Defined Functions (directly loading Basic’s Standard library, and creating respective tokens, when parsing spreadsheets, but not doing that with other languages - so all other languages’ functions can’t be used as UDFs, only as implementations called from UDFs written in Basic). This is a matter of implementation, and could be changed if wanted (but any language can be used directly in extension-based custom functions, so why change this?).

1 Like

5 Likes

Thanks for the feedback the preface has been updated.

1 Like

So you need to update also this dead link

What’s the summary solution to this? I want to be able to install something and then use python commands in spreadsheets.

The summary would be a loop: Start again with the first comment:

I don’t think this us currently possible, as LibreOffice does not pack a full python in its install. Also the is a lot more to consider, as not everithing in python is necessarily a function.
.
Maybe look for something like Jupyter Notebooks…

A Potential solution would be to add listeners to the sheet.

This way when something changes in the chosen cells the listener can be notified and other changes can be made via python.

Take a look at Python LibreOffice Programming Calc Chapter 25. Monitoring Sheets to see if this idea could work for you.

Some string functions? Apache OpenOffice Community Forum - [Calc][oxt] A function for all python string methods - (View topic)
Playing games with numbers? PySudoku » Extensions

Interesting. Also a Suduku example here without an extension.