栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在OpenOffice.Org Calc的单元格公式中调用Python宏?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在OpenOffice.Org Calc的单元格公式中调用Python宏?

在旧的OO.org论坛上,(超级)用户Villeroy发布了有关如何从OO.org
Basic
调用Python函数的说明,然后可将其用于公式中。关键是将

com.sun.star.script.provider.MasterscriptProviderFactory
服务用作桥梁。这是他的解决方案的改编版,可概括为在任意模块中调用任意函数:

REM Keep a global reference to the scriptProvider, since this stuff may be called many times: Global g_MasterscriptProvider as ObjectREM Specify location of Python script, providing cell functions: Const URL_Main as String = "vnd.sun.star.script:" Const URL_Args as String = "?language=Python&location=user"Function invokePyFunc(file AS String, func As String, args As Array, outIdxs As Array, outArgs As Array)   sURL = URL_Main & file & ".py$" & func & URL_Args   oMSP = getMasterscriptProvider()   On Local Error GoTo ErrorHandler      oscript = oMSP.getscript(sURL)      invokePyFunc = oscript.invoke(args, outIdxs, outArgs)      Exit Function   ErrorHandler:      Dim msg As String, toFix As String      msg = Error$      toFix = ""      If 1 = Err AND InStr(Error$, "an error occurred during file opening") Then         msg = "Couldn' open the script file."         toFix = "Make sure the 'python' folder exists in the user's scripts folder, and that the former contains " & file & ".py."      End If      MsgBox msg & chr(13) & toFix, 16, "Error " & Err & " calling " & funcend FunctionFunction getMasterscriptProvider()    if isNull(g_MasterscriptProvider) then       oMasterscriptProviderFactory = createUnoService("com.sun.star.script.provider.MasterscriptProviderFactory")       g_MasterscriptProvider = oMasterscriptProviderFactory.createscriptProvider("")    endif    getMasterscriptProvider = g_MasterscriptProviderEnd Function

然后可以使用它来创建可在公式中调用的OO.org Basic函数。使用示例

pytype

Const libfile as String = "util"    REM functions live in util.pyFunction pytype(value)    pytype = invokePyFunc(libfile, "pytype", Array(value), Array(), Array())End Function

另一个可能的实现是创建一个Python加载项。但是,这是一个繁重的选择,因为它需要安装OpenOffice
SDK,并且对于这种方法是适用于免费功能还是仅适用于类,这对我来说并不明显。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/626168.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号