I’ve written a basic macro for calc that calls a python script to do a calculation and then return the result to the cell that calls the macro. It works just fine except when opening the spreadsheet.
When opening the spreadsheet (during startup) ThisDocument (in basic) and XSCRIPTCONTEXT.getDocument() are both null. I managed to fix the basic error by moving the script from “My Macros” into the actual .ods in the Organize Macros menu, but then the python script still errors out because XSCRIPTCONTEXT.getDocument() returns nothing. I can call the macro again by re-entering the call in the cell and it works just fine. This appears to be a known bug: https://bugs.documentfoundation.org/show_bug.cgi?id=92770
I can’t move the python script into the document (instead of “My Macros”) because then the basic script can’t find it - I can’t find any way to customize this script URI in basic in order to point it to the document itself: “vnd.sun.star.script:Cell_Functions.py$calcEffectValue?language=Python&location=user”
Is there a way to delay script initialization until the document has fully loaded and ThisDocument returns a proper value? Or is there some other way to load the document? In the python script, I’m only accessing the document to get my named ranges from it, are there any alternative ways to get this data?
doc = XSCRIPTCONTEXT.getDocument()
nameDict = {}
for range in doc.NamedRanges:
nameDict[range.getName()] = range.getReferredCells().getDataArray()[0][0]
I get the error: “‘NoneType’ object has no attribute ‘NamedRanges’”, because doc is null.
Basic macro that calls the python script:
function cev(a as String) as double
Dim scriptPro As Object, myScript As Object, currentCom As Object
scriptPro = ThisComponent.getScriptProvider()
myScript = scriptPro.getScript( _
"vnd.sun.star.script:Cell_Functions.py$calcEffectValue?language=Python&location=user")
cev = myScript.invoke(Array(a), Array(), Array())
end function
LibreOffice Version: 6.3.4.2 (x64)
Python 3.8