Script provider loads document python macros too late?

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
ms777
Volunteer
Posts: 177
Joined: Mon Oct 08, 2007 1:33 am

Script provider loads document python macros too late?

Post by ms777 »

Hi,

I am trying to use python macros as cell functions with Basic wrappers.
My python function, stored in the document (returns the working directory):

Code: Select all

import os
def getWorkingDirectory():
    return os.getcwd()
g_exportedScripts = (getWorkingDirectory,)
My Basic wrapper:

Code: Select all

function getWorkingDirectory()
	GlobalScope.BasicLibraries.LoadLibrary("XrayTool")
	oScriptProvider = ThisComponent.getScriptProvider()
'	xray oScriptProvider.ChildNodes
	oScript = oScriptProvider.getScript("vnd.sun.star.script:getCwd.py$getWorkingDirectory?language=Python&location=document")
	getWorkingDirectory = oScript.invoke(array(), array(), array())
End function
In Sheet1!A1 I call

Code: Select all

=GETWORKINGDIRECTORY()
If I open the document, I get an Error creating provider from factory error. Once if the document is loaded, I can extend the formula to say A2, and everything is running fine.
Same behavior on AO and LO.

With the xray line, you can see that at the first call the beanshell / javascript / python script providers are not yet loaded ...

Do I do something wrong here?

ms777
Attachments
LibreOrApache.ods
(10.69 KiB) Downloaded 83 times
Bidouille
Volunteer
Posts: 577
Joined: Mon Nov 19, 2007 10:58 am
Location: France

Re: Script provider loads document python macros too late?

Post by Bidouille »

ms777
Volunteer
Posts: 177
Joined: Mon Oct 08, 2007 1:33 am

Re: Script provider loads document python macros too late?

Post by ms777 »

... thanks for the pointer to the AO bug. P5 means that it will never be solved, I suppose.

In LO, things are a bit more complex: if I store the python in user, not document, and call by

Code: Select all

'	oScriptProvider = ThisComponent.getScriptProvider()
	oScriptProvider = CreateUNOService("com.sun.star.script.provider.MasterScriptProviderFactory").createScriptProvider("")
	xray oScriptProvider.ChildNodes
	oScript = oScriptProvider.getScript("vnd.sun.star.script:getCwd.py$getWorkingDirectory?language=Python&location=user")
Xray does not show the PythonScriptprovider, but the formula function executes without error.

In LO bugzilla I found this: https://bugs.documentfoundation.org/sho ... ?id=143989

Could some non Windows users doublecheck the issue ?

ms777
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Script provider loads document python macros too late?

Post by JeJe »

Is the problem just stopping the error on opening the document?

Code: Select all

global boo as boolean
function getWorkingDirectory()
if not boo then 
boo = true
else
	oScriptProvider = ThisComponent.getScriptProvider()
	oScript = oScriptProvider.getScript("vnd.sun.star.script:getCwd.py$getWorkingDirectory?language=Python&location=document")
	getWorkingDirectory = oScript.invoke(array(), array(), array())
	msgbox getWorkingDirectory
end if
End function

Edit:

Or

Code: Select all

function getWorkingDirectory()
on error goto hr
	oScriptProvider = ThisComponent.getScriptProvider()
	oScript = oScriptProvider.getScript("vnd.sun.star.script:getCwd.py$getWorkingDirectory?language=Python&location=document")
	getWorkingDirectory = oScript.invoke(array(), array(), array())
	msgbox getWorkingDirectory
hr:
End function
The macro works for me on the "view created" document event but not on "document loading finished."
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
ms777
Volunteer
Posts: 177
Joined: Mon Oct 08, 2007 1:33 am

Re: Script provider loads document python macros too late?

Post by ms777 »

Hi,

thanks for the workaround. I am more interested to understand why the formula does not work on first call, and if this is an LO/AO bug or programmer stupidity :-)

I have not coupled the macro to any event. It is just referenced by the user defined function call in A1

ms777
Bidouille
Volunteer
Posts: 577
Joined: Mon Nov 19, 2007 10:58 am
Location: France

Re: Script provider loads document python macros too late?

Post by Bidouille »

ms777 wrote: Thu Dec 22, 2022 6:41 pm P5 means that it will never be solved, I suppose.
If you don't comment and vote for the issue, you suppose correctly.
ms777
Volunteer
Posts: 177
Joined: Mon Oct 08, 2007 1:33 am

Re: Script provider loads document python macros too late?

Post by ms777 »

Bidouille wrote: Thu Dec 22, 2022 9:45 pm
ms777 wrote: Thu Dec 22, 2022 6:41 pm P5 means that it will never be solved, I suppose.
If you don't comment and vote for the issue, you suppose correctly.
I added a comment. I did not find a voting button, though.

I discovered some strange behavior: If you take the attached LibreOrApache.ods, select all cells, right click on the row headers and select a fixed cell height (not optimum height!) everything works without error ...

Cheers ms777

Edit: in LO it works without error, in AO this did not help
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Script provider loads document python macros too late?

Post by JeJe »

ms777 wrote: Wed Dec 28, 2022 2:04 pm I discovered some strange behavior: If you take the attached LibreOrApache.ods, select all cells, right click on the row headers and select a fixed cell height (not optimum height!) everything works without error ...
I still get the error message on opening - LO 7.4.0.3 , Windows 10
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Jurassic Pork
Posts: 24
Joined: Wed Oct 25, 2017 7:55 am
Location: France

Re: Script provider loads document python macros too late?

Post by Jurassic Pork »

Hello,
I have found a workaround :
call the python script only if thiscomponent is loaded and recalculate all when thisComponent is loaded :

Code: Select all

function getWorkingDirectory()
        getWorkingDirectory = ""
	if thisComponent.isLoaded then
	oScriptProvider = ThisComponent.getScriptProvider()
	oScript = oScriptProvider.getScript("vnd.sun.star.script:getCwd.py$getWorkingDirectory?language=Python&location=document")
	getWorkingDirectory = oScript.invoke(array(), array(), array())
        end if
End function
macro for Event Open Document :

Code: Select all

Public Sub ComponentLoaded()
ThisComponent.calculateall()
End Sub
if i do a loop to wait thisComponent.isLoaded in the getWorkingDirectory function i have an infinite loop.

Friendly, J.P
OpenOffice 4.1.14 , LibreOffice 7.6.2.1 on Windows 11/ LibreOffice 7.3.7 on Lubuntu 22.04
User avatar
karolus
Volunteer
Posts: 1159
Joined: Sat Jul 02, 2011 9:47 am

Re: Script provider loads document python macros too late?

Post by karolus »

Hallo

Code: Select all

from pathlib import Path
def getWorkingDirectory():
    return f"{Path.cwd()}"
use pathlb instead os.path
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
Post Reply