在旧的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,并且对于这种方法是适用于免费功能还是仅适用于类,这对我来说并不明显。



