【问题标题】:How can I reset the color of a Google Sheet through the API如何通过 API 重置 Google Sheet 的颜色
【发布时间】:2020-12-22 02:17:47
【问题描述】:

基于this question 关于设置颜色,我想知道如何重置/清除 Google 表格标签的颜色。

供参考,这里是如何设置颜色

    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。

还有一个“tabColorStyle”参数,但它也在寻找颜色。

我尝试将“tabColor”设置为空字典,{},RGB 分别设置为 0,RGB 设置为 -1。所有最终都只是将颜色变为黑色。

没有提到.clear 选项。

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

这是Google Sheet APISheet properties 的链接,我一直在其中查找请求的外观。

【问题讨论】:

    标签: python google-sheets google-sheets-api gspread


    【解决方案1】:

    我相信你的目标如下。

    • 您想要重置 Google 电子表格中工作表的标签颜色。
    • 您想使用 gspread 实现此目的。

    修改点:

    • 在这种情况下,我认为使用fields 的值很重要。当"fields": "tabColor"用于batchUpdate方法的请求体时,修改tabColor的属性。在这种情况下,为了重置标签颜色,tabColor 不包含在properties 中。这样,标签颜色就会被重置。

    当上述点反映到脚本中时,变为如下。

    示例脚本:

    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 电子表格中所有工作表的选项卡颜色均已重置。

    注意:

    • 当您想重置 Google 电子表格中一张表的标签颜色时,请使用以下请求正文。

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

    参考资料:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-27
      • 2023-02-12
      相关资源
      最近更新 更多