Scriptforge GridControl, python, Error 91, Bug or Feature?

Hi,

i did create a simple Dialog with one grid control. When I call the example code from an embedded module it worls fine. When I move the source to a local file and change the onaction from

onAction = "vnd.sun.star.script:Module1.py$actionHandler?language=Python&location=document"

to

onAction = "vnd.sun.star.script:Module1.py$actionHandler?language=Python&location=user"

I get Error 91 with the text

Library :   ScriptForge
Service :   SF_Services
Method :    CreateScriptService


The ScriptForge library has crashed. The reason is unknown.
Maybe a bug that could be reported on
    https://bugs.documentfoundation.org/

More details : 

Location : SF_Services.CreateScriptService/169
Objektvariable nicht belegt.

And later when I close the dialog:
THE EXECUTION IS CANCELLED.

Error during invoking function data_map in module file:///home/dominik/.config/libreoffice/4/user/Scripts/python/ModuleKonto_200.py (<class 'RuntimeError'>: The execution of the method 'Execute' failed. Execution stops.
  File "/usr/lib/libreoffice/program/pythonscript.py", line 941, in invoke
    ret = self.func(*args)
  File "/home/dominik/.config/libreoffice/4/user/Scripts/python/ModuleKonto_200.py", line 334, in data_map
    dlgGrid()
    ~~~~~~~^^
  File "/home/dominik/.config/libreoffice/4/user/Scripts/python/ModuleKonto_200.py", line 313, in dlgGrid
    dlg.Execute()
    ~~~~~~~~~~~^^
  File "/usr/lib/libreoffice/program/scriptforge.py", line 2187, in Execute
    return self.ExecMethod(self.vbMethod + self.flgHardCode, 'Execute', modal)
           ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/libreoffice/program/scriptforge.py", line 641, in ExecMethod
    return self.EXEC(self.objectreference, flags, methodname, *args)
           ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/libreoffice/program/scriptforge.py", line 366, in InvokeBasicService
    raise RuntimeError("The execution of the method '" + method + "' failed. Execution stops.")
)

When I move the Dialog from the document into the standard library and call it with
dlg = CreateScriptService(“SFDialogs.Dialog”, “GlobalScope”, “Standard”, “Dialog1”)
i still get the same error?

Full Example Code:

# coding: utf-8
from __future__ import unicode_literals

from scriptforge import CreateScriptService

exc = CreateScriptService("Exception")
bas = CreateScriptService("Basic")
doc = CreateScriptService("Calc")


def debug(msg) -> None:
    #bas.MsgBox('Hello, world!')
    exc.DebugPrint('Debug Msg: ', msg) # this sends the values to the SF console
    exc.Console() # this displays the SF console
    #exc.PythonShell({**globals(), **locals()}) # this opens the APSO shell
    #sprint('Value of someVar', someVar) # the values are sent to the APSO shell

onAction = "vnd.sun.star.script:Module1.py$actionHandler?language=Python&location=document"

def actionHandler(event: None) ->None:
    #debug(__file__ + ":OnAction")
    if event == None:
        return
    try:
        dlg = CreateScriptService("DialogEvent", event)
        #debug(f"A Name: {dlg.Name} Control Type: {dlg.ControlType} Event: {event}")
        #debug(dlg.ListIndex)
        idx = doc.LastRow("Tabelle1") + 1      # Next free line
        doc.SetValue("Tabelle1.A"+str(idx), str(bas.Now()))
        doc.SetValue("Tabelle1.B"+str(idx), f"Name {dlg.Name}")
        doc.SetValue("Tabelle1.C"+str(idx), f"Control Type {dlg.ControlType}")
        doc.SetValue("Tabelle1.D"+str(idx), f"ListIndex {dlg.ListIndex}")
        doc.SetValue("Tabelle1.E"+str(idx), f"Value {dlg.Value}")
        doc.SetValue("Tabelle1.F"+str(idx), f"Value {event}")

    except Exception as e:
        debug(f"actionHandler error {e}")


def dlgGrid() -> None:
    dlg = CreateScriptService("SFDialogs.Dialog", "SF_GridCtrl.ods", "Standard", "Dialog1")
    grid = dlg.Controls("GridControl1")
    #grid.OnMousePressed = onAction
    grid.OnMouseReleased = onAction
    grid_data = (("Column A", "Column B", "Column C"),
              ("Row 1", 1, 2),
              ("Row 2", 3, 4),
              ("Row 3", 5, 6))
    alignments = "LCC"
    widths = (50, 50, 50)
    grid.SetTableData(grid_data, widths, alignments)

    dlg.Execute()
    dlg.Terminate()


g_exportedScripts = (dlgGrid,)

if __name__ == '__main__':
    pass

If it matters, i have the same behaviour on MacOS and Linux and I am using the latest LO version:
Version: 25.8.3.2 (X86_64) / LibreOffice Community
Build ID: 5ee70f9e60a556cf41458d71cbfa54885591385a
CPU threads: 32; OS: Linux 6.17; UI render: default; VCL: kf6 (cairo+wayland)
Locale: de-DE (de_DE.UTF-8); UI: de-DE
25.8.3-2.1
Calc: threaded

Thanks for any advice

Could you please upload the document with the dialog and the python script both embedded ?
(Remove any confidential data, if any).

SF_GridCtrl.ods (20.5 KB)

Hope this helps. The .ods contains sample dialog and module - it works as far I can judge ok. When you export the Module1 to user space and change the onaction target to the user space, the error happens when you click into the grid.

Thank you!

https://wiki.documentfoundation.org/Documentation/DevGuide/Scripting_Framework#Scripting_Framework_URI_Specification

Maybe, but I do not understand it.
When I use the list box example from the Scriptforge Wiki, it uses this code to call the event:

onaction = 'vnd.sun.star.script:Module1.py$ProcessLists?language=Python&location=user'

This works as expected.
The difference I see is that the dialog is created in the code and not using an existing dialog

Hint: Please make sure that “actionHandler” method is explicitly declared within g_exportedScripts global variable

1 Like

Meanwhile i get the feeling that multiple CreateScriptService calls might cause my issue?

exc = CreateScriptService("Exception")
bas = CreateScriptService("Basic")
#doc = CreateScriptService("Calc")
#g_exportedScripts = (dlgGrid, actionHandler, )
g_exportedScripts = (dlgGrid,)
OK

exc = CreateScriptService("Exception")
bas = CreateScriptService("Basic")
doc = CreateScriptService("Calc")
#g_exportedScripts = (dlgGrid, actionHandler, )
g_exportedScripts = (dlgGrid,)
ERROR 

exc = CreateScriptService("Exception")
bas = CreateScriptService("Basic")
doc = CreateScriptService("Calc")

g_exportedScripts = (dlgGrid, actionHandler, )
#g_exportedScripts = (dlgGrid,)
ERROR 

exc = CreateScriptService("Exception")
#bas = CreateScriptService("Basic")
doc = CreateScriptService("Calc")
g_exportedScripts = (dlgGrid, actionHandler, )
#g_exportedScripts = (dlgGrid,)
ERROR

exc = CreateScriptService("Exception")
#bas = CreateScriptService("Basic")
#doc = CreateScriptService("Calc")
g_exportedScripts = (dlgGrid, actionHandler, )
#g_exportedScripts = (dlgGrid,)
OK

exc = CreateScriptService("Exception")
#bas = CreateScriptService("Basic")
#doc = CreateScriptService("Calc")
#g_exportedScripts = (dlgGrid, actionHandler, )
g_exportedScripts = (dlgGrid,)
OK

#exc = CreateScriptService("Exception")
#bas = CreateScriptService("Basic")
doc = CreateScriptService("Calc")
#g_exportedScripts = (dlgGrid, actionHandler, )
g_exportedScripts = (dlgGrid,)
ERROR 

The number of CreateScriptService() calls does not matter.
You get an error every time you execute:

doc = CreateScriptService("Calc")

The onAction statement is correct.

What is wrong, then ?

When you run the dlgGrid() script, the python module is loaded and the

doc = CreateScriptService("Calc")

is executed successfully, considering the current window. The dialog is opened normally and waits until the

dlg.Execute()

is finished.

When you click in the gridcontrol, the python module is re-loaded. It is a new instance (the 1st instance is still in execution). And

doc = CreateScriptService("Calc")

is re-executed. This statement aborts because the script cannot identify the current document from a dialog.

Correction:

Replace

doc = CreateScriptService("Calc")

with one of next statements:

doc = CreateScriptService("Calc", 'SF_GridCtrl.ods')
doc = CreateScriptService("Calc", bas.ThisComponent)

I would recommend to do this also wen the module is stored inside the document.

2 Likes

Thank you for your solution, much appreciated. It started to work for me after I moved “doc = CreateScriptService(“Calc”, ‘SF_GridCtrl.ods’)” into the eventHandler