首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google Sheet通过API实现颜色交替

Google Sheet通过API实现颜色交替
EN

Stack Overflow用户
提问于 2017-06-06 11:56:48
回答 0查看 710关注 0票数 2

在Python中使用sheets API时,我尝试使用交替颜色设置工作表的格式。在UI中,这可以在Format > Alternating colors...

根据我所能找到的,这是通过使用banding的API完成的。不幸的是,我还没有找到一个有效的例子来说明这是如何做到的。下面是我构建的值字典,颜色值目前并不重要,我只想让它为工作表着色。

代码语言:javascript
复制
requests = {
  'bandedRange': {
    'bandedRangeId': 1,
    'range': {
      'sheetId': 0,
      'startRowIndex': 0,
      'endRowIndex': len(values),
      'startColumnIndex': 0,
      'endColumnIndex': 4,
    },
    'rowProperties': {
      'headerColor': {
        'red': 1,
        'green': 0,
        'blue': 1,
        'alpha': 1,
      },
      'firstBandColor': {
        'red': 1,
        'green': 0,
        'blue': 0,
        'alpha': 0,
      },
      'secondBandColor': {
        'red': 0,
        'green': 1,
        'blue': 0,
        'alpha': 0,
      }
    },
  },
  'fields': '*',
}
body = {'requests': requests}
response = service.spreadsheets().batchUpdate(spreadsheetId=spreadsheet_id, body=body).execute()

此操作失败,并显示以下错误:

代码语言:javascript
复制
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/oauth2client/_helpers.py", line 133, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/googleapiclient/http.py", line 840, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://sheets.googleapis.com/v4/spreadsheets/$spreadsheet_id:batchUpdate?alt=json returned "Invalid JSON payload received. Unknown name "banded_range" at 'requests': Cannot find field.">

我很确定我的问题是字段的值,但是我找不到一个有效的例子来说明在这里应该使用什么。如果我完全省略了字段键,我会得到同样的错误。

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44381181

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档