I have a spreadsheet that I would like to use a Python macro in. I have written Python coding, but never in a macro in OpenOffice.
The code is simply a counter that counts up with a 3 second delay. I need the count to display in a specific cell, say, E6.
---------------------------------------------------------------------------------------------------------------
import time
a = 0
b = 500
while a < b:
a = a + 1
print(a)
time.sleep(3)
--------------------------------------------------------------------------------------------------------------
Any help would be greatly appreciated.
Thanks.
[Solved] Simple Python Macro help please
[Solved] Simple Python Macro help please
Last edited by Hagar Delest on Mon Sep 29, 2014 10:16 pm, edited 1 time in total.
Reason: tagged [Solved].
Reason: tagged [Solved].
OpenOffice 4.1
Re: Simple Python Macro help please
Not even one reply...for shame.
However, I was able to figure it out. If anyone needs a counter that counts up in single increments and has a "wait" here it is.
However, I was able to figure it out. If anyone needs a counter that counts up in single increments and has a "wait" here it is.
Code: Select all
Sub Main
Dim oDoc As Object
Dim oSheet As Object
oDoc=ThisComponent
oSheet=oDoc.Sheets.getByName("Sheet1")
oCell=oSheet.getCellRangeByName("d20")
a=0
Do
a=a+1
oCell.SetString(a)
Wait 2500
Loop While a < 10
End Sub
OpenOffice 4.1
Re: [Solved] Simple Python Macro help please
Py function is not too complicated. Like this:But be warned: unlike in Basic, the Python function in OO/LO is run inside single UI cycle. That means that the interface is blocked/dead until the function exits.
"Responsive" behaviour could be achieved by more sophisticated objective programming.
Code: Select all
import time
def q():
doc = XSCRIPTCONTEXT.getDocument()
sheet = doc.CurrentController.ActiveSheet
cell = sheet.getCellByPosition(4,0)
a = 0
b = 500
while a < b:
a += 1
cell.setValue(a)
time.sleep(3)
"Responsive" behaviour could be achieved by more sophisticated objective programming.
Last edited by Jan_J on Mon Sep 29, 2014 10:34 pm, edited 1 time in total.
JJ ∙ https://forum.openoffice.org/pl/
LO (25.2|24.8) ∙ Python (3.12|3.10) ∙ Unicode 16 ∙ LᴬTEX 2ε ∙ XML ∙ Unix tools ∙ Linux (Rocky|CentOS)
LO (25.2|24.8) ∙ Python (3.12|3.10) ∙ Unicode 16 ∙ LᴬTEX 2ε ∙ XML ∙ Unix tools ∙ Linux (Rocky|CentOS)
Re: [Solved] Simple Python Macro help please
Thank you Jan. I much prefer Python so I guess I need to figure out how to input it into Calc.
OpenOffice 4.1
Re: [Solved] Simple Python Macro help please
If you would have learned the most fundamental basics about macro programming in Python ("hello world" to a cell), you could have written this within a minute.
Edit: ... and you would have learned that you can not do threading in Python macros. |
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Re: [Solved] Simple Python Macro help please
Using separate thread in Python code fixes the problem.
Code: Select all
import time
from threading import Thread
def cnt(amax, dt):
doc = XSCRIPTCONTEXT.getDocument()
sheet = doc.CurrentController.ActiveSheet
cell = sheet.getCellByPosition(4,0)
a = 0
while a < amax:
a += 1
cell.setValue(a)
time.sleep(dt)
def startCnt():
t = Thread(target = cnt, args = (500, 3))
t.start()
g_exportedScripts = (startCnt, )
JJ ∙ https://forum.openoffice.org/pl/
LO (25.2|24.8) ∙ Python (3.12|3.10) ∙ Unicode 16 ∙ LᴬTEX 2ε ∙ XML ∙ Unix tools ∙ Linux (Rocky|CentOS)
LO (25.2|24.8) ∙ Python (3.12|3.10) ∙ Unicode 16 ∙ LᴬTEX 2ε ∙ XML ∙ Unix tools ∙ Linux (Rocky|CentOS)