Search code examples
pythonvariablesgoogle-sheetsgspread

Issues with adding a variable to python gspread


I have started to use the gspread library and have sheet already that I'd like to append after the last row that has data in it. I'll retrieve the values between A1 and maxrows to loop through them and check if they are empty. However, I am unable to add a variable to the second line here. But perhaps I am just not escaping it correct? I bet this is very simple:

maxrows = "A" + str(worksheet.row_count)
cell_list = worksheet.range('A1:A%s') % (maxrows)

Solution

  • Your variable maxrows already is in the form of "An", the concatenation already contains the letter and the number

    But you are adding an extra A to it here worksheet.range('A1:A%s')

    Also you're not using the string interpolation correctly with % (in your code you are not applying % to the range string)

    It should have been one of these

    maxrows = "A" + str(worksheet.row_count)
    worksheet.range('A1:%s' % maxrows)
    

    or

    worksheet.range('A1:A%d' % worksheet.row_count)
    

    (among other possible solutions)