Getting the sheet in Python addin



I sent this to the list 01/07. I am resending with a modification .
Would there be any interest in me putting the addin into the Gnumeric source as a plugin ?



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 different sheet, there is no problem, the rangeref has a sheet attribute that I 
can query.
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 the sheet from 
which the function is called. 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.08 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 downloaded and 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']()  # calls 'get_EvalSheet' to get the current sheet
           wb = Gnumeric.Workbooks()[0]  
           range_sheet = wb.get_sheets()[n]
    else:
           range_sheet = theRange.start.sheet
..........................



My Makefile

--------------------------------------------------------------------------------------------------------------------------------------
PLUGIN = GetEvalSheet

GNUMERIC_SOURCE =  -I/home/ninds/temp/gnumeric-1.7.8 \
       -I/home/ninds/temp/gnumeric-1.7.8/src
# when I downloaded the gnumeric source I unziped it at  /home/ninds/temp/
       
GNUMERIC_HEADERS = -I/home/ninds/opt/include/libspreadsheet-1-7 \
       -I/usr/include/libgoffice-0.3 
       
# 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 \
    -I/usr/include/libgsf-1 
    
all :
gcc -fPIC -shared -o $(PLUGIN).so \
       $(GNUMERIC_SOURCE)\
       $(GNUMERIC_HEADERS)\
       $(GNOME_CFLAGS) GetEvalSheet.c
        
   
-------------------------------------------------------------------------------------------------------------------------------------

My plugin.xml

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

--------------------------------------------------------------------------------------------------------------------------------------------------
The C plugin

--------------------------------------------------------------------------------------------------------------------------------------------------

#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>


GNM_PLUGIN_MODULE_HEADER;

static GnmFuncHelp const help_getEvalSheet[] = {
{ GNM_FUNC_HELP_OLD,
        F_("@FUNCTION=get_EvalSheete\n"
           "@SYNTAX=get_EvalSheet()\n"
           "@DESCRIPTION= Gets the sheet from where the function is evalued"
           )
},
{ GNM_FUNC_HELP_END }
};
static GnmValue *
get_EvalSheet (GnmFuncEvalInfo *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,
   GNM_FUNC_SIMPLE, GNM_FUNC_IMPL_STATUS_COMPLETE, GNM_FUNC_TEST_STATUS_BASIC },
   {NULL}
};

----------------------------------------------------------------------------------------------------------------------------------------

Send instant messages to your online friends http://uk.messenger.yahoo.com


      ___________________________________________________________
Yahoo! Answers - Got a question? Someone out there knows the answer. Try it
now.
http://uk.answers.yahoo.com/ 



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