如何通过API重置Google表格的颜色 [英] How can I reset the color of a Google Sheet through the API

查看:111
本文介绍了如何通过API重置Google表格的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关设置颜色的问题为基础,我想知道如何重置/清除Google表格的颜色标签.

Building off of this question about setting a color, I wonder how to reset/clear the color of a Google Sheet's tab.

作为参考,这是设置颜色的方法

For reference, here is how to set a color

    sheet = open_worksheet() # does all the auth/credential work
    new_tab = sheet.worksheet('DemoTab')

    body = {
        "requests": [
            {
                "updateSheetProperties": {
                    "properties": {
                        "sheetId": new_tab.id,
                        "tabColor": {
                            "red": 1.0,
                            "green": 0.3,
                            "blue": 0.4
                        }
                    },
                    "fields": "tabColor"
                }
            }
        ]
    }

    try:
        res = sheet.batch_update(body)
        pprint(res)
    except gspread.exceptions.APIError as gea:
        pprint(gea.args[0], width=100)

所有文档均声明"tabColor"应该是一个Color对象(如上所示,带有红色,绿色和蓝色的字典).还有一个可选的Alpha.

All the documentation states that "tabColor" should be a Color object (as shown above with a dict of red, green, and blue). There is also an optional alpha.

还有一个"tabColorStyle"标签,参数,但它也在寻找颜色.

There is also a "tabColorStyle" parameter, but it too is looking for a color.

我尝试设置"tabColor"到一个空的dict, {} ,RGB分别为0,RGB分别为-1.最终所有结果只是将颜色变成黑色.

I have tried setting "tabColor" to an empty dict, {}, RGB to 0 each, and RGB to -1 each. All end up just turning the color black.

没有提及 .clear 选项.

那么一旦设置了颜色,我该如何删除呢?

So how do I remove the color once it has been set?

此处是 Google表格API Sheet属性,我一直在这里查找请求应该看起来.

Here is a link to the Google Sheet API and Sheet properties where I've been looking up how the request should look.

推荐答案

我相信您的目标如下.

  • 您想重置Google Spreadsheet中工作表的标签颜色.
  • 您想使用gspread实现这一目标.
  • 在这种情况下,我认为使用 fields 的值很重要.当将"fields":"tabColor" 用于batchUpdate方法的请求主体时,将修改 tabColor 的属性.在这种情况下,为了重置选项卡颜色, properties 中不包含 tabColor .这样,选项卡的颜色将被重置.
  • In this case, I think that to use the value of fields is an important point. When "fields": "tabColor" is used for the request body of the method of batchUpdate, the property of tabColor is modified. In that case, in order to reset the tab color, tabColor is not included in properties. By this, the tab color is reset.

当上述要点反映到脚本时,它如下所示.

When above point is reflected to the script, it becomes as follows.

spreadsheetId = "###" # Please set the Spreadsheet ID.

client = gspread.authorize(credentials)
spreadsheet = client.open_by_key(spreadsheetId)
sheets = spreadsheet.worksheets()
body = {"requests": [{
    "updateSheetProperties": {
        "properties": {
            "sheetId": e.id,
        },
        "fields": "tabColor"
    }
} for e in sheets]}
spreadsheet.batch_update(body)

  • 在此示例脚本中,将重置Google Spreadsheet中所有工作表的标签颜色.
    • 当您要重置Google Spreadsheet中某张工作表的标签颜色时,请使用以下请求正文.

    • When you want to reset the tab color of one of sheets in the Google Spreadsheet, please use the following request body.

      body = {"requests": [{
          "updateSheetProperties": {
              "properties": {
                  "sheetId": sheetId, # Please set the sheet ID.
              },
              "fields": "tabColor"
          }
      }]}
    

    这篇关于如何通过API重置Google表格的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆