How to find the first empty row of a google spread sheet using python GSPREAD?

前端 未结 5 1088
既然无缘
既然无缘 2020-12-05 11:26

I am struggling to write codes that find me the first empty row of a google sheet.

I am using gspread package from github.com/burnash/gspread

I would be glad

5条回答
  •  被撕碎了的回忆
    2020-12-05 12:07

    I solved this using:

    def next_available_row(worksheet):
        str_list = list(filter(None, worksheet.col_values(1)))
        return str(len(str_list)+1)
    
    scope = ['https://spreadsheets.google.com/feeds']
    credentials = ServiceAccountCredentials.from_json_keyfile_name('auth.json', scope)
    gc = gspread.authorize(credentials)
    worksheet = gc.open("sheet name").sheet1
    next_row = next_available_row(worksheet)
    
    #insert on the next available row
    
    worksheet.update_acell("A{}".format(next_row), somevar)
    worksheet.update_acell("B{}".format(next_row), somevar2)
    

提交回复
热议问题