I have a macro loop in Calc written in both Libreoffice Basic, and in python (via Scriptforge). The python code runs much slower, I think because it updates the Calc screen with each calculation, whereas the Basic code disables screen updating by default (it seems to me). It is a big complex spreadsheet and it takes 28 seconds for 100 simulations with LIbreoffice Basic code and and about three times that for Python via Scriptforge doing exactly the same thing. I have pasted my code below. Please tell me how to disable screen updating using Scriptforge (or provide some other tip to speed up the python/Scriptforge code). I am using Libreoffice 7.4, running on Debian.
Edit: My workaround solution is switch to a worksheet that has no values to update. The python code then runs just as fast as the Basic code.
Here is my Basic code:
Sub Simulation
REM copy paste section
i = 0
Do Until i = 100
REM copy configured
oCopyCell = ThisComponent.Sheets.GetByName("Simulation_Config").GetCellRangeByPosition(5,4,31,4)
oPasteCell = ThisComponent.Sheets.GetByName("Output").GetCellRangeByPosition(1,4+i,27,4+i)
oPasteCell.DataArray = oCopyCell.DataArray
REM copy CYOA activation
oCopyCell = ThisComponent.Sheets.GetByName("Simulation_Config").GetCellRangeByPosition(5,14,31,14)
oPasteCell = ThisComponent.Sheets.GetByName("Output").GetCellRangeByPosition(30,4+i,56,4+i)
oPasteCell.DataArray = oCopyCell.DataArray
REM update counter
oCopyCell = ThisComponent.Sheets.GetByName("Simulation_Config").GetCellRangeByName("C6")
oPasteCell = ThisComponent.Sheets.GetByName("Simulation_Config").GetCellRangeByName("C5")
oPasteCell.DataArray = oCopyCell.DataArray
i = i + 1
Loop
End Sub
Here is my Python code:
from scriptforge import CreateScriptService
def run_simulation(args=None):
# the following line is necessary to invoke service
doc = CreateScriptService("Calc")
i=0
y=doc.GetValue("Simulation_Config.C4")
while i < y:
trackingValues=doc.GetValue("Simulation_Config.F5:AF5")
targetCell = doc.Offset("Output.B5:AB5",0+i,0)
doc.SetValue(targetCell, trackingValues)
nextSim =doc.GetValue("Simulation_Config.C6")
currentSim=doc.Offset("Simulation_Config.C5",0,0)
doc.SetValue(currentSim, nextSim)
i = i+1
g_exportedScripts = (
run_simulation,
)
[edit by karolus: “fenced” code with three backticslines]