Calc Funktion mit Python und optionalen Argumenten
Verfasst: Mi 7. Mai 2014, 21:40
Hallo zusammen,
Ich habe ein paar Pythonfunktionen geschrieben, die ich als Zellfunktionen für Calc verwenden kann. Ich habe mich dabei an Instruktionen der Seiten
http://de.openoffice.info/viewtopic.php?f=2&t=53706 und http://stackoverflow.com/questions/7591 ... e-org-calc gehalten.
Eigentlich klappt das Ganze wunderbar. ABER was mache ich, wenn ich eine Pythonfunktion mit optinalen Argumenten habe?
In der Standard Basic Bib [Meine Makros & Dialoge].Standard habe ich dann folgenden eingetragen:
Ich habe ein paar Pythonfunktionen geschrieben, die ich als Zellfunktionen für Calc verwenden kann. Ich habe mich dabei an Instruktionen der Seiten
http://de.openoffice.info/viewtopic.php?f=2&t=53706 und http://stackoverflow.com/questions/7591 ... e-org-calc gehalten.
Eigentlich klappt das Ganze wunderbar. ABER was mache ich, wenn ich eine Pythonfunktion mit optinalen Argumenten habe?
Code: Alles auswählen
# python file: pyfuncs.py
def normalfunc(arg):
# some code here
return val
def funcWithOptArgs(arg1, arg2, optarg1="foo", optarg2="bar"):
# some code here
return val
Code: Alles auswählen
Global g_MasterScriptProvider
Const pymodule as String = "pyfuncs.py"
Function getMasterScriptProvider()
if NOT isObject(g_MasterScriptProvider) then
s = "com.sun.star.script.provider.MasterScriptProviderFactory"
g_MasterScriptProvider = createUnoService(s).createScriptProvider("")
endif
getMasterScriptProvider = g_MasterScriptProvider
End Function
Function inPy(func As String, args As Array)
s = "vnd.sun.star.script:" & pymodule & "$" & func & "?language=Python&location=user"
On Local Error GoTo ErrorHandler
inPy = getMasterScriptProvider().getScript(s).invoke(args, Array(), Array())
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 " & pymodule
End If
MsgBox msg & chr(13) & toFix, 16, "Error " & Err & " calling " & func
end Function
'#########
' WRAPPER
Function normalfunc(arg)
normalfunc = inPy("normalfunc", Array(arg))
End Function
Function funcWithOptArgs(arg1, arg2, optarg1, optarg2)
' was ist mit den optionalen Argumenten hier?
funcWithOptArgs = inPy("funcWithOptArgs", Array(arg1, arg2, optarg1, optarg2))
End Function