Seite 1 von 1

Calc Funktion mit Python und optionalen Argumenten

Verfasst: Mi 7. Mai 2014, 21:40
von jg-n-py
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?

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
In der Standard Basic Bib [Meine Makros & Dialoge].Standard habe ich dann folgenden eingetragen:

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


Re: Calc Funktion mit Python und optionalen Argumenten

Verfasst: Mi 7. Mai 2014, 22:09
von karolus
Hallo

Im Prinzip musst du die Basicseite erweitern auf bsplw.

Code: Alles auswählen

Function superexponent( Wert, optional decimals )
   sURL = URL_Main & "superexponent" & URL_Args
   oMSP = getMasterScriptProvider()
   oScript = oMSP.getScript(sURL)
   if ismissing( decimals) then
   		x = oScript.invoke( Array( Wert ),Array(),Array())
   else:
   		x = oScript.invoke( Array( Wert, decimals ),Array(),Array())
   end if
   superexponent = x
end function
oder baue deine Funktionen gleich hier ein - damit hast ein paar Möglichkeiten zusätzlich und ausserdem später die Unterstützung durch den Formelassistenten.

Karolus

Re: Calc Funktion mit Python und optionalen Argumenten

Verfasst: Mi 7. Mai 2014, 22:41
von jg-n-py
Vielen Dank Karolus.

Ich habe mich bisher noch nicht an eigene Add-Ons herangewagt - da dafür mMn auch mehr Einarbeitungszeit nötig ist.

Aber auf die Schnelle werde ich erst mal den Ansatz über den IF-Test im Basicwrapper probieren.

Vielen Dank