openoffice calc python函数不修改sh

2024-10-02 16:26:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经定义了一个函数hellotest,该函数定义如下,将在OpenOffice calc下执行。 此函数只在单元格(3,4)中写入Hello单词。 当函数作为宏执行时,工作表会被修改,但当函数通过名为HELLOTEST的基本函数执行时,不会发生任何事情(见下文)。 python函数的调用似乎是因为它返回的字符串“Hello world”显示在调用单元格中,但单元格(3,4)没有修改。 我应该理解python函数只能作为宏执行,还是应该以不同的方式使用XScript上下文

Python:

import uno

def hellotest():
   oSheet = XSCRIPTCONTEXT.getDocument().getSheets().getByIndex(0)
   oCell1 = oSheet.getCellByPosition(3,4) 
   oCell1.setString("Hello world")
   return "Hello world"

基本功能:

REM  *****  BASIC  *****
REM Keep a global reference to the ScriptProvider, since this stuff may be     called many times:
Global g_MasterScriptProvider

REM Specify location of Python script providing the cell functions:
Const URL_Main = "vnd.sun.star.script:hellotest.py$"
Const URL_Args = "?language=Python&location=user"

Function getMasterScriptProvider()
   if NOT isObject(g_MasterScriptProvider) then
      oMasterScriptProviderFactory =     createUnoService("com.sun.star.script.provider.MasterScriptProviderFactory")
      g_MasterScriptProvider = oMasterScriptProviderFactory.createScriptProvider("")
   endif
   getMasterScriptProvider = g_MasterScriptProvider
End Function

Function HELLOTEST(s$, optional iLen%)
   sURL = URL_Main & "hellotest" & URL_Args
   oMSP = getMasterScriptProvider()
   oScript = oMSP.getScript(sURL)
   x = oScript.invoke(Array(),Array(),Array())
   HELLOTEST = x
End Function`

Tags: 函数urlhelloworld定义scriptfunctionarray