Python table to calc via basic function

Hello,
I would like to create a function that populates a table under the cursor. Let me explain better: I have a python function that outputs a table for the sake of having the simplest example lets say I have a python function that returns a 10x10 random matrix e.g.

def myfn():
    import numpy as np
    return np.random.randn(10,10).tolist()

What I would like to do, is to create a function say mycalcfunc that simply calls the python function and places the generated table under the cursor meaning that the top left edge of the table is the selected cell.

How do I do this? I have been strugling with basic.

Thanks a lot in advance!

Maybe this link will help you to call the python function from StarBasic.

If you want tocreate a Cell Function (but not a StarBasic Subroutine), then you must use a two dimensional Array as output variable:

Array function written in Python: PySudoku » Libreoffice Extensions
The extension is tagged “Calc, Fun, Macro”. However, a function add-in has nothing to do with macro programming. Add-in functions appear in the function wizard, in the side bar but not in the macro organizer.
You need to install the LibreOffice SDK.
If I remember correctly, I wrote the interface definition sources/XPySudoku.idl, compiled XPySudoku.urd file from that and finally got XPySudoku.rdb somehow.
The add-in compilation was more sophisticated than writing the Python code, which I borrowed with consent from Peter Norvig Solving Every Sudoku Puzzle. I just added some type conversion from string to tuple and dict to tuple.

Nobody forces you to use basic!


import numpy as np

def main(*_):
    rlist = np.random.randn(10,5).tolist()
    doc = XSCRIPTCONTEXT.getDocument()
    sel = doc.CurrentSelection
    sheet = sel.Spreadsheet
    cursor = sheet.createCursorByRange(sel)
    cursor.collapseToSize(5,10)
    cursor.DataArray = rlist