[Solved] Simple Python Macro help please

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Jtrader
Posts: 29
Joined: Thu Oct 24, 2013 3:39 pm

[Solved] Simple Python Macro help please

Post by Jtrader »

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.
Last edited by Hagar Delest on Mon Sep 29, 2014 10:16 pm, edited 1 time in total.
Reason: tagged [Solved].
OpenOffice 4.1
Jtrader
Posts: 29
Joined: Thu Oct 24, 2013 3:39 pm

Re: Simple Python Macro help please

Post by Jtrader »

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.

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
Jan_J
Posts: 187
Joined: Wed Apr 29, 2009 1:42 pm
Location: Poland

Re: [Solved] Simple Python Macro help please

Post by Jan_J »

Py function is not too complicated. Like this:

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)
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.
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)
Jtrader
Posts: 29
Joined: Thu Oct 24, 2013 3:39 pm

Re: [Solved] Simple Python Macro help please

Post by Jtrader »

Thank you Jan. I much prefer Python so I guess I need to figure out how to input it into Calc.
OpenOffice 4.1
User avatar
Villeroy
Volunteer
Posts: 31345
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved] Simple Python Macro help please

Post by Villeroy »

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
Jan_J
Posts: 187
Joined: Wed Apr 29, 2009 1:42 pm
Location: Poland

Re: [Solved] Simple Python Macro help please

Post by Jan_J »

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)
Post Reply