Big problem with Python functions . not knowing the sheet.


I have been wanting to add user defined functions to Python, but there has been major hurdle.
If I call a Python function with a parameter that is a range from the same sheet the function is being called 
from, I have trouble getting at the cell data. This is because to access cells I need to know the sheet. If 
the range of cells is from another sheet, there is no problem, the rangeref has a sheet attribute that I can 
But if it he range is on the same sheet as the function this is NULL. 

To solve this , what I have done is written a C function that simply returns the index of this sheet. We can 
then call ths function from our python function and know which sheet the function is evaluated from.

I already had  gnumeric 1.7.0 installed on an Ubuntu box.
I downloaded the source for gnumeric from the gnumeric website and unzipped it.
I then ran 

    ./configure  --prefix=~/opt

I didn't  want to damage my existing installation, I only needed the source. configure showed up alot of 
missing libraries which I downlodaed an installed.

I then ran configure again, successfully.followed by 'make' and 'make 'install'.

The plugin adds the function 'get_EvalSheet()' to Gnumeric. In your python function you can then do something 
like this.

def MyFunc(theRange):

    if theRange.start.sheet == None:
           n = Gnumeric.functions['get_EvalSheet']()
           wb = Gnumeric.Workbooks()[0]  
           range_sheet = wb.get_sheets()[n]
           range_sheet = theRange.start.sheet

My Makefile

PLUGIN = GetEvalSheet

GNUMERIC_SOURCE =  -I/home/ninds/temp/gnumeric-1.7.0 \
# when I downloaded the gnumeric source I unziped it at  /home/ninds/temp/
GNUMERIC_HEADERS = -I/home/ninds/opt/include/libspreadsheet-1-7 \
# I installed it at /home/ninds/opt/
GNOME_CFLAGS =  -I/usr/include/glib-2.0 \
    -I/usr/lib/glib-2.0/include \
    -I/usr/include/libxml2 \
    -I/usr/include/pango-1.0 \
all :
 gcc -fPIC -shared -o $(PLUGIN).so \
       $(GNOME_CFLAGS) GetEvalSheet.c

My plugin.xml

<?xml version="1.0"?>
<plugin id="GetEvalSheet">
  <name>GetEvalSheet  plugin</name>
  <description>Plugin contains a function return the sheet on which a function evaluation is taking place 
 <loader type="Gnumeric_Builtin:module">
  <attribute value="GetEvalSheet" name="module_file"/>
  <service type="function_group" id="Ninds">
   <category xml:lang="en">Gnumeric</category>
    <function name="get_EvalSheet"/>


#include <gnumeric-config.h>
#include <gnumeric.h>
#include <func.h>
#include <str.h>
#include <cell.h>
#include <sheet.h>
#include <value.h>
#include <rangefunc.h>
#include <gnm-i18n.h>
#include <gnm-plugin.h>


static GnmFuncHelp const help_getEvalSheet[] = {
           "@DESCRIPTION= Gets the sheet from where the function is evalued"
static GnmValue *
get_EvalSheet (FunctionEvalInfo *ei, GnmValue const * const *argv)
 return  value_new_int (ei->pos->sheet->index_in_wb);

const GnmFuncDescriptor Ninds_functions[] = {
 { "get_EvalSheet", "", N_(""),
   help_getEvalSheet,  get_EvalSheet, NULL, NULL, NULL, NULL,


Send instant messages to your online friends

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]