如何使用python GSPREAD找到谷歌电子表格的第一行空行? [英] How to find the first empty row of a google spread sheet using python GSPREAD?

查看:73
本文介绍了如何使用python GSPREAD找到谷歌电子表格的第一行空行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力编写代码,让我找到 Google 表格的第一行空行.

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

我正在使用来自 github.com/burnash/gspread 的 gspread 包

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

如果有人能帮忙我会很高兴:)

I would be glad if someone can help :)

我目前刚刚导入了模块并打开了工作表

I currently have just imported modules and opened the worksheet

scope = ['https://spreadsheets.google.com/feeds']

credentials = ServiceAccountCredentials.from_json_keyfile_name('ddddd-61d0b758772b.json', scope)

gc = gspread.authorize(credentials)

sheet = gc.open("Event Discovery")
ws = sheet.worksheet('Event Discovery')

我想用函数找到第1158行,它是工作表的第一个空行,这意味着每次填充旧的空行时,它都会找到下一个空行见这里

I want to find row 1158 which is the first empty row of the worksheet with a function, which means everytime the old empty row is filled, it will find the next empty row See here

推荐答案

我使用以下方法解决了这个问题:

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)

这篇关于如何使用python GSPREAD找到谷歌电子表格的第一行空行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆