Gspread 批量更新回溯

Gspread batch update traceback

我在尝试预填充 google 工作表过滤器然后执行 batch_update 时遇到问题。我得到了正确的执行,导入了 table,创建了过滤器。当批量更新发生时,我得到一个我不明白的回溯。我认为回溯可能来自我对 sheetId 进行编码的方式。如果有人对如何阅读回溯有一些智慧,以便我可以解决潜在的问题,我将不胜感激。

我正在使用 python 3.10 和 gspread 5.3.2,我的过滤器设置来自我对基础 Google Sheets batchUpdate 方法的阅读。

请指教, 蒂姆

Gspread-batchUpdate Google Sheets batchUpdate

Traceback (most recent call last):
  File "/Users/peiffer/sre-cert-manager/scripts/get-certificates2.py", line 282, in <module>
    main()
  File "/Users/peiffer/sre-cert-manager/scripts/get-certificates2.py", line 268, in main
    import_csv_to_gsheet(output_file, google_api_credentials)
  File "/Users/peiffer/sre-cert-manager/scripts/get-certificates2.py", line 215, in import_csv_to_gsheet
    worksheet.batch_update(body)
  File "/Users/peiffer/venv/lib/python3.10/site-packages/gspread/utils.py", line 600, in wrapper
    return f(*args, **kwargs)
  File "/Users/peiffer/venv/lib/python3.10/site-packages/gspread/worksheet.py", line 848, in batch_update
    data = [
  File "/Users/peiffer/venv/lib/python3.10/site-packages/gspread/worksheet.py", line 849, in <listcomp>
    dict(vr, range=absolute_range_name(self.title, vr["range"])) for vr in data
TypeError: string indices must be integers

代码片段如下:

#/usr/bin/env python3.10

name = 'MySheet'
gc = gspread.service_account(filename=path_to_credentials)

try:
    spreadsheet = gc.open(name)
except gspread.SpreadsheetNotFound:
    spreadsheet = gc.create(name)
    sharewith = '<email>'
    spreadsheet.share(sharewith, perm_type='user',
                      notify=True, role='writer')
results = gc.import_csv(spreadsheet.id, open(
    filepath, 'r').read().encode('utf-8'))
_ = results
worksheet = spreadsheet.sheet1
#
# Misc code here
#
worksheet.sort((1, 'asc'), range='A3:M16000')
worksheet.set_basic_filter(name='A2:M16000')
status_values_to_exclude = ["Expired", "Revoked"]
filter_specs = [{"columnIndex": 3, "filterCriteria": {"hiddenValues": status_values_to_exclude}}]
grid_range = gspread.utils.a1_range_to_grid_range('A2:M16000')
grid_range['sheetId'] = worksheet._properties['sheetId']
body = {"requests": [{"setBasicFilter": {"filter": {"range": grid_range, "filter_specs": filter_specs}}}]}
worksheet.batch_update(body)

您好,我相信您正在寻找的正确方法是 batch_update but spreadsheet 对象中,而不是在 worksheet 对象中.

您发送的似乎是您手动构建的完整请求,worksheet 对象中的方法 batch_update 仅用于更新值。

你应该使用:

...
body = {"requests": [{"setBasicFilter": {"filter": {"range": grid_range, "filter_specs": filter_specs}}}]}
spreadsheet.batch_update(body)