python



Yes, you can iterate over cells in the Python API.

It'll be better when RangeRef isn't opaque, but you can do it. If you know
the cells you can do it directly, and you can get some cell stuff from
Gnumeric.

But if you've got a RangeRef right now you need a little work.
Here's some code to unpack a RangeRef and use it in a Sum function. 

def range_ref_to_tuples(range_ref):
        '''I need a function to find the bounds of a RangeRef. This one
        extracts them from the Gnumeric "column" and "row" commands, and
        returns them as a pair of tuples. Surely there is a better way?
        For example, return a list of cells??'''

        col  = Gnumeric.functions['column']   
        row  = Gnumeric.functions['row']

        # "column" and "row" take references and return an array of col or row
        # nums for each cell in the reference. For example, [[1, 1, 1], [2, 2, 2]]
        # for columns and [[2, 3, 4], [2, 3, 4]] for rows.

        try:
                columns = col(range_ref)
                rows    = row(range_ref)

                begin_col = columns[0][0] - 1  
                begin_row = rows[0][0] - 1     

                end_col = columns[-1][-1]
                end_row = rows[-1][-1]

                # We subtracted 1 from the begin values because in the API,
                # indexing begins at 0, while "column" and "row" begin at 1.
                # We did NOT subtract 1 from the end values, in order to make
                # them suitable for Python's range(begin, end) paradigm.
                
        except TypeError:
                raise GnumericError,GnumericErrorVALUE
        except NameError:                     # right name?
                raise GnumericError,Gnumeric.GnumericErrorNAME
        except RefError:                     # right name?
                raise GnumericError,Gnumeric.GnumericErrorREF
        except NumError:                     # right name?
                raise GnumericError,Gnumeric.GnumericErrorNUM


        return [ (begin_col, begin_row), (end_col, end_row) ]
        
        
        
def func_sum2(gRange):
        '@FUNCTION=PY_SUM2\n'\
        '@SYNTAX=PY_SUM2(range)\n'\
        '@DESCRIPTION=Adds a range of numbers together,'\
        'without calling built-in SUM.\n\n'\
        '@EXAMPLES=To add values in A1 to A5, just type them in:\n'\
        '    py_sum(a1:a5)\n'\
        '@SEEALSO='
        try:
                [r_begin, r_end] = range_ref_to_tuples(gRange)
                wb=Gnumeric.Workbooks()[0]   # Careful! This is WRONG! It doesn't
                s=wb.get_sheets()[0]         # use the ACTUAL workbook or sheet.

                val = 0
                for col in range(r_begin[0], r_end[0]):
                        for row in range(r_begin[1], r_end[1]):
                                cell = s[col, row]
                                val = val + cell.get_value()
                                # Note: this doesn't skip blank cells etc.

        except TypeError:
                raise GnumericError,GnumericErrorVALUE
        else:
                return val





--
Charles R. Twardy, Res.Fellow,  Monash University, School of CSSE
ctwardy at alumni indiana edu   +61(3) 9905 5823 (w)  5146 (fax)

"Incongruous places often inspire anomalous stories." -- S.J. Gould





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