python - 为谷歌表格中一列的每个单元格获取颜色代码
问题描述
我正在尝试使用 pygsheets 获取 google sheet 的每个单元格的颜色代码。正在使用的代码块:
import pygsheets
gc = pygsheets.authorize(service_file='credentials.json')
sh = gc.open('golden dataset')
wks = sh[0]
count = 1
color_code = []
while count<27628:
cell = pygsheets.Cell('J'+str(count),worksheet=wks)
color_code.append(cell.color)
其中 27628 是列长度 & 'golden dataset' 是工作表名称 & credentials.json 有助于将 google_sheets 与 python 连接起来。虽然,这工作得很好,但是对于一列来说非常慢(大约需要 7-8 小时)。有没有更快的方法来做到这一点?谢谢
解决方案
您可以使用 get_all_valuescells
作为返回类型。
import pygsheets
gc = pygsheets.authorize(service_file='credentials.json')
sh = gc.open('golden dataset')
wks = sh[0]
cells = wks.get_all_values(returnas='cell', include_tailing_empty=False, include_tailing_empty_rows=False)
# adjust if you need trailing empty cells too
color_code = []
for r in cells:
for c in r:
color_code.append(c.color)
如果您想以自定义批次获取数据,您可以与和get_values
一起使用。wks.rows
wks.cols