python functions with multi-type params fails to load with Gnumeric spreadsheet
$ gnumeric ff.gnumeric Failed to load py_sum2 Failed to load py_twoDiffTypeParams
In this case, the python function is expecting a string and a float as parameters.
Gnumeric spreadsheet has the following function call in cell B13 with referenced cell B4 containing an integer:
=py_twoDiffTypeParams("u",B4)
function definition and plugin.xml coding syntax taken from Gnumeric help and Python Hacks - 7. Notes on Spreadsheets etc
python file my-func.py
contains the following function:
# my-func.py
#
from Gnumeric import GnumericError, GnumericErrorVALUE
import Gnumeric
import string
import re
def func_twoDiffTypeParams(strStr, num1):
'@FUNCTION=PY_TWODIFFTYPEPARAMS\n'\
'@SYNTAX=PY_TWODIFFTYPEPARAMS(strStr, num1nge)\n'\
'@DESCRIPTION=Returns +/- num1 based on strStr="l"/"u".'\
'@EXAMPLES=Specify a string as either l or u and cell reference containing a number:\n'\
' py_twoDiffTypeParams("l",b3) returns minus value of b2\n'\
'@SEEALSO='
try:
# Get the contents of the cell as a string
#cell_value = func_cell_value(ref_range)
if 'l' == strStr:
return_val = -1 * num1
elif 'u' == strStr:
return_val = num1
except TypeError:
raise GnumericError, GnumericErrorVALUE
except NameError: # right name?
raise GnumericError, GnumericErrorNAME
finally:
return return_val
# Translate the func_add python function to a gnumeric function and register it
example_functions = {
'py_add': ('ff','num1,num2',func_add),
'py_sum': ('r', 'values', func_sum),
'py_range_ref_to_tuples': ('r', 'values', range_ref_to_tuples),
'py_sum2': ('r' 'values', func_sum2),
'py_numInStr': ('r', 'values', func_numInStr),
'py_cell_value': ('r', 'values', func_cell_value),
'py_func_twoDiffTypeParams': ('sf', 'sentence, num1', func_twoDiffTypeParams)
}
The file plugin.xml
is as follows:
<?xml version="1.0"?>
<plugin id="Gnumeric_MyFuncPlugin">
<information>
<name>Other Python functions from HOWTO</name>
<description>A few extra python functions demonstrating the API.</description>
</information>
<loader type="Gnumeric_PythonLoader:python">
<attribute name="module_name" value="my-func"/>
</loader>
<services>
<service type="function_group" id="example">
<category>Local Python</category>
<functions>
<function name="py_add"/>
<function name="py_sum"/>
<function name="py_sum2"/>
<function name="py_range_ref_to_tuples"/>
<function name="py_numInStr"/>
<function name="py_cell_value"/>
<function name="py_twoDiffTypeParams"/>
</functions>
</service>
</services>
</plugin>
```[ff.gnumeric](/uploads/d0cc9c7883b1c852c3dc697a7a4c00f0/ff.gnumeric)[my-func.py](/uploads/b331669eefb19c0586d34e05e97546fa/my-func.py)[plugin.xml](/uploads/e54c2288f38b06fadd30fabf15c9095f/plugin.xml)![Gnumeric_Plugin_Manager](/uploads/a38153fdc27e02c33c73335e38446993/Gnumeric_Plugin_Manager.png)
Edited by calkusi