Home > front end >  LibreOffice: embed script in script URL
LibreOffice: embed script in script URL

Time:12-05

In LibreOffice, It is possible to run python scripts like this:

sURL = "vnd.sun.star.script:file.function?language=Python&location=document"
oScript = scriptProv.getScript(sURL)
x = oScript.Invoke(args, Array(), Array())

In that example 'file' is a filename, and 'function' is the name of a function in that file.

Is it possible to embed script in that URL? sURL="vnd.." & scriptblock & "?language.."

(It seems like the kind of thing that might be possible with the correct URL, or might not be possible if just not supported).

CodePudding user response:

We can use Python's eval() function. Here is an example inspired by JohnSUN's explanation in the discussion. Note: xray() uses XrayTool to show output, but you could replace that line with any output method of your choosing, such as writing to a file.

def runArbitraryCode(*args):
    url = args[0]
    codeString = url.split("&codeToRun=")[1]
    x = eval(codeString)
    xray(x)

Now enter this formula in Calc and Ctrl click on it.

=HYPERLINK("vnd.sun.star.script:misc_examples.py$runArbitraryCode?language=Python&location=user&codeToRun=5 1")

Result: 6

Obligatory caveat: Running eval() on an unknown string is about the worst idea imaginable in terms of security. So hopefully you're the one controlling the URL and not some black hat hacker!

  • Related