Search code examples
pythondjangogspread

while loop incrementing range in django


I am using gspread library to read cell values from Google spreadsheets. I want to loop over the range like:

col_index=3
i=0
while i<4:
    cell_values_list=[worksheet.cell(r, c).value for r in range(10,20) for c in range(col_index,col_index+3)]
    ...
    ...
    col_index=col_index+3
    i=i+1

instead of using row column values, i want to use

worksheetworksheet.range('C10:E20')

and in the next iteration i want

worksheetworksheet.range('F10:H20')

and so on.

can anyone please help me how can i iterate or say increment my column value


Solution

  • You can use get_addr_int(row, col) function which will output a string label for your cell, see the docs here: https://gspread.readthedocs.org/en/latest/#gspread.Worksheet.get_addr_int

    If I get our code right in your case it will be something like:

    col_index=1
    i=0
    while i<4:
        start_cell = worksheetworksheet.get_addr_int(10, col_index)
        end_cell = worksheetworksheet.get_addr_int(20, col_index+2)
        cell_values_list=worksheetworksheet.range('%s:%s' % (start_cell, end_cell))
        ...
        ...
        col_index=col_index+3
        i=i+1