【问题标题】:fetching color code for each cell of a column in google sheets为谷歌表格中一列的每个单元格获取颜色代码
【发布时间】:2021-09-15 07:00:53
【问题描述】:

我正在尝试使用 pygsheets 获取 google 工作表的每个单元格的颜色代码。正在使用的代码块:

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 是列长度,“黄金数据集”是工作表名称,并且 credentials.json 有助于将 google_sheets 与 python 连接起来。虽然,这工作得很好,但是对于一列来说非常慢(大约需要 7-8 小时)。有没有更快的方法来做到这一点?谢谢

【问题讨论】:

    标签: python google-sheets colors pygsheets


    【解决方案1】:

    您可以使用带有cells 作为返回类型的get_all_values。

    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.rowswks.cols

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多