We will be migrating from Ask to Discourse on the first week of August, read the details here

Ask Your Question

Return value from Macro [closed]

asked 2018-05-17 16:55:14 +0200

stromo gravatar image

updated 2021-07-03 23:26:49 +0200

Alex Kemp gravatar image

I've written a Python script which geocodes a location.

I would like to execute this on a cell (e.g. K2) and write back the result to K1 (latitude) and K2 (longitude).

Sub test

    Cell = oSheet.GetCellRangeByName("K183")
    v = Cell.getString

    x = Shell ("python /work/mapping/calc-lati.py" & v)

    MsgBox x

End Sub

I'm always getting back "0", so that's probably the exit code for success. But how can I really get the cli output of my script?

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Alex Kemp
close date 2021-05-10 21:14:17.359600



Is there a reason you didn't write the whole macro in Python-UNO? The only reason I can think of is if calc-lati.py requires libraries that are not included in the python core.

Even if that is the case, I recommend you write the macro in Python, because that will make it easier to obtain a result through interprocess communication. Basic doesn't do this well.

Jim K gravatar imageJim K ( 2018-05-18 00:27:22 +0200 )edit

Here on Pastebin is my script; it only imports requests, json & sys.

But as I am a total noob I would like to ask you for a minimal working example, on which I could build up my script. May you please do this?

stromo gravatar imagestromo ( 2018-05-22 23:18:54 +0200 )edit

3 Answers

Sort by » oldest newest most voted

answered 2018-05-23 05:12:21 +0200

Jim K gravatar image

updated 2018-05-23 05:13:34 +0200

Here is a working example. Use APSO to run this code.

import json
import os
import sys
import urllib.request

import uno

def test():
    oDoc = XSCRIPTCONTEXT.getDocument()
    oSheet = oDoc.getCurrentController().getActiveSheet()
    oCell = oSheet.getCellRangeByName("K183")
    longitude = "123"
    latitude = "456"
    oCell.setString(longitude + ", " + latitude)

g_exportedScripts = test,

The requests library is not included with LO python on my Windows system. Perhaps urllib.request does what you need instead.

edit flag offensive delete link more

answered 2018-05-23 16:03:19 +0200

Xoristzatziki gravatar image

Calling shell will not return the outcome of an executable. To call a python script "involved" with LO you must either include it as LO script or as user script or as document script.

My older tries to use requests (for its benefits) where a nightmare since requests is not included in standard LO python.

Anyway your /work/mapping/calc-lati.py can write the answer in a predefined file (say: /work/mapping/calc-lati.outcome), you have to wait for shell to return (using bsync) and then read that file and set the Value or String of any cells you want.

edit flag offensive delete link more

answered 2018-05-20 18:10:11 +0200

You may have to use the bSync parameter to have Shell wait until the shell command is finished to return. See https://wiki.openoffice.org/wiki/Docu....

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2018-05-17 16:55:14 +0200

Seen: 806 times

Last updated: May 23 '18