从 LibreCalc 中调用 python 函数 [英] Calling a python function from within LibreCalc

查看:38
本文介绍了从 LibreCalc 中调用 python 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在名为 calculadora.py 的文件中有以下代码

I have the following code inside a file called calculadora.py

def calcpy(numA,numB):
    return numA+numB

在 LibreCalc 中调用函数 calcpy() 的正确方法是什么?我想把,说,在单元格 A1=calcpy(B1,C1) 并获得结果......

Which is the proper way of calling the function calcpy() from within LibreCalc? I'd like to put, say, in cell A1=calcpy(B1,C1) and obtain the result...

我已经将 python 脚本放在 /usr/lib/libreoffice/share/Scripts/python//home/lucas/.config/libreoffice/4/user/Scripts/python,我也可以在菜单Tools->Macro->Organize Macro->Python 下看到它

I've already put the python script in both /usr/lib/libreoffice/share/Scripts/python/ and /home/lucas/.config/libreoffice/4/user/Scripts/python, I can also see it under the menu Tools->Macro->Organize Macro->Python

...但我真的不知道如何调用该函数.

... but I do not really know how to invoke that function.

我读过一些建议将函数包装在 Basic 中的帖子,例如:

I've read some posts where it was suggested to wrap the function inside Basic, such as this:

function calcpy(a as double, b as double) as double
    script = GetScript("calcpy")
    calcpy = script.invoke(Array(a, b), Array(), Array())
end function

...但是每当我使用 =calcpy(5,4) 时,我都会收到一条错误消息:BASIC 运行时错误.未定义子过程或函数过程."

... but whenever I do use =calcpy(5,4) I get an error saying: "BASIC runtime error. Sub-procedure or function procedure not defined."

这甚至可能吗?使用 Basic 是正确的方法吗?有没有更直接(优雅?)的方法?

Is this even possible? Is using Basic the proper way? Is there a more direct (elegant?) way of doing it?

谢谢,

卢卡斯

推荐答案

这里有一个很好的答案:如何在 OpenOffice.Org Calc 中的单元格公式中调用 Python 宏? 在您的情况下,基本代码应该是:

There is a good answer here: How can I call a Python macro in a cell formula in OpenOffice.Org Calc? In your case, the Basic code should be:

function calcpy(a as double, b as double) as double
    Dim scriptPro As Object, myScript As Object
    scriptPro = ThisComponent.getScriptProvider()
    myScript = scriptPro.getScript( _
           "vnd.sun.star.script:calculadora.py$calcpy?language=Python&location=user")
    calcpy = myScript.invoke(Array(a, b), Array(), Array() )
end function

是的,有一个更直接/更优雅的方式:创建一个加载项.

Yes, there is a more direct / elegant way: Create an Add-In.

这篇关于从 LibreCalc 中调用 python 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆