Gsheet Python。追加一行并忽略第一列。怎么样?

问题描述 投票:0回答:2
import gspread
from oauth2client.service_account import ServiceAccountCredentials

scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']

credentials = ServiceAccountCredentials.from_json_keyfile_name('TTOOL-f0b223d454c13.json', scope)

gc = gspread.authorize(credentials)

wks = gc.open("Log").sheet1

for s in gc.openall():
    print (s.title)

wks.append_row(["A", "B", "C"])

我尝试删除“ A”。仍然走到最底部。我只想忽略第一列。任何想法如何做到这一点?

python python-3.x google-api gspread pygsheets
2个回答
1
投票
import gspread
from oauth2client.service_account import ServiceAccountCredentials

def next_available_row(wks):
    str_list = list(filter(None, wks.col_values(2)))  # fastest
    return str(len(str_list)+1)

scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']

credentials = ServiceAccountCredentials.from_json_keyfile_name('TOOL-fsbd3df3.json', scope)

gc = gspread.authorize(credentials)


wks = gc.open("Log").sheet1

next_row = next_available_row(wks)




wks.update_acell("B{}".format(next_row), "test")
wks.update_acell("C{}".format(next_row), "test")

弄清楚了。请享用。


0
投票

为时已晚,但是有相同的问题,使用None可以解决:

wks.append_row([None, 'B', 'C'])
© www.soinside.com 2019 - 2024. All rights reserved.