Search code examples
python-2.7cellgoogle-sheetsgspread

gspread or such: help me get cell coordinates (not value)


By using GSpread I have a range of cell's returned from a google spreadsheet that has all its elements something like this:

<Cell R1C1 'Sandero'>

I know how to get from here the cell value:

cell.value

but I would also like to get the address (R1C1 in this case) separately. It would have to be (R1,C1) or even better if I can get it in (1,1) format (maybe by using a dictionary?)

You see I need the address as well because I will later declare a graph with these values.

I'm using Python 2.7 and latest Gspread library.

Any help would be greately appreciated.


Solution

  • The best way to get a cell's coordinates is by using the cell's instance properties row and col. This way your code can be simplified to this:

    for cell in cell_list:
        print cell.value
        print "Row:", cell.row, "Column:", cell.col
    

    To get a tuple, just wrap it in parenthesis: (cell.row, cell.col).

    Since I'm the author of gspread, I've updated the library page to make it clear how to get a cell's coords. Thanks for pointing out the missing part.