问题:我正在尝试创建一个对一系列电子表格值进行操作并返回单个值的Python UDF。 研究:根据我从数小时的谷歌搜索中收集到的信息,我需要一个调用我的 python 脚本的基本包装器。 这是我的包装纸:
Function Call_mode_freq(range as string) as double
Dim oScriptProvider, oScript
oScriptProvider = ThisComponent.getScriptProvider()
oScript = oScriptProvider.getScript(_
"vnd.sun.star.script:stat_functions.py$mode_freq? language=Python&location=user")
Call_mode_freq = oScript.invoke(array(range), array(), array() )
End Function
我使用以下公式调用 UDF:=CALL_MODE_FREQ("A2:B11")
将范围作为字符串传递似乎是一个问题。 我创建了一个名为 stat_functions.py 的模块,它有一个名为 mode_freq 的函数 我尝试过使用 uno XSCRIPTCONTEXT 和 ScriptForge 接口,但两者都无法工作。 这是我不完整的 python 函数:
def mode_freq(arange):
# arange is a string e.g. "A3:B10"
# uno interface #####################################
oSheet = XSCRIPTCONTEXT.getDocument().getSheets().getByIndex(0)
oCell = oSheet.getCellByPosition(0,0)
# I want to get the values from "arange"
# ScriptForge interface #######################
doc = CreateScriptService("Calc")
# crange = doc.Sheets["Sheet1"][arange] # this fails. The error message says I'm supposed to specify a tuple of integers not a string.
# again, no idea how to get the values from a range
mode = 0 # dummy value to ensure something is returned to Calc cell with this UDF
return mode
我找不到有关这些接口的对象模型的任何文档(即文档或工作表)。 包装器正在调用 python 脚本,并且我的虚拟返回值正在使用公式写入单元格,因此该部分正在工作。 希望有人能指出我正确的方向。
我认为您的代码是正确的,但需要进行一些优化。尝试一下