Thà*_*Đạt 11 python python-2.7 gspread
我正在努力编写代码,发现我是谷歌表的第一个空行.
我正在使用github.com/burnash/gspread的gspread包
如果有人可以帮助我会很高兴:)
我目前刚刚导入了模块并打开了工作表
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')
Run Code Online (Sandbox Code Playgroud)
我想找到行1158,它是带有函数的工作表的第一个空行,这意味着每次填充旧的空行时,它将找到下一个空行. 请参阅此处
Ped*_*ito 19
我解决了这个问题:
def next_available_row(worksheet):
str_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)
Run Code Online (Sandbox Code Playgroud)
这种替代方法通过考虑可能跳过值的行(例如文档中的精美标题部分)以及对前N列进行采样来解决已接受答案的问题:
def next_available_row(sheet, cols_to_sample=2):
# looks for empty row based on values appearing in 1st N columns
cols = sheet.range(1, 1, sheet.row_count, cols_to_sample)
return max([cell.row for cell in cols if cell.value]) + 1
Run Code Online (Sandbox Code Playgroud)