You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LibreOffice BASIC调用返回列表的Python函数遇运行错误求助

在LibreOffice BASIC中调用返回列表的Python脚本无错误运行方案

问题背景

使用LibreOffice BASIC调用返回列表的Python函数(如def MyFunc(a,b): return [a,b])时,原参考代码出现「BASIC runtime error. Argument is not optional」的outArgs参数错误;尝试传递空数组后无报错,但无法获取有效返回值,invoke返回对象的lbound=0且ubound=-1。

无错误运行的实现方案

要确保调用无错误,需正确初始化invoke方法的输出参数,以下是可行的BASIC代码示例:

Function invokePythonFunc(a As Variant, b As Variant) As Variant
    Dim oScriptProvider As Object
    Dim oScript As Object
    Dim args(1) As Variant
    Dim outIdxs As Variant
    Dim outArgs As Variant
    
    ' 构造传入Python函数的参数
    args(0) = a
    args(1) = b
    ' 必须显式初始化输出参数为空数组
    outIdxs = Array()
    outArgs = Array()
    
    ' 获取脚本服务提供者
    oScriptProvider = ThisComponent.getScriptProvider()
    ' 替换为你的Python脚本的实际URI
    oScript = oScriptProvider.getScript("vnd.sun.star.script:MyModule.py$MyFunc?language=Python&location=user")
    
    ' 执行调用,返回Python函数的结果
    invokePythonFunc = oScript.invoke(args, outIdxs, outArgs)
End Function

关键注意事项

  • 必须显式将outIdxs和outArgs初始化为Array(),不能传递未声明或未初始化的数组变量,否则会触发参数错误
  • 若仅需无错误运行而不需要处理输出参数,直接传递空数组即可,无需后续读取outArgs
  • Python返回的列表会自动转换为LibreOffice BASIC的数组,可通过函数返回值直接获取

内容的提问来源于stack exchange,提问作者david

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 07:25:35