使用python更新Google电子表格的单元格背景颜色 [英] Update cell background color for google spreadsheet using python

查看:149
本文介绍了使用python更新Google电子表格的单元格背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Google API通过batchupdate函数更新电子表格中单元格的背景颜色. https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/batchUpdate 如果用值value和背景色red更新单元格sheet1!A1,我的请求应该是什么样?

I wanna use google api to update the background color of a cell in a spreadsheet with batchupdate function. https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/batchUpdate what should my request be like if to update the cell sheet1!A1 with the value value and background color red.

推荐答案

  • 您要使用Sheets API的batchUpdate方法将Google Spreadsheet上工作表名称"Sheet1"中的单元格"A1"的背景颜色更改为红色.
  • 您要使用带有python的google-api-python-client实现此功能.
  • 您已经能够使用Sheets API获取和放置Google Spreadsheet的值.
  • 如果我的理解是正确的,那么这个答案如何?请认为这只是几个可能的答案之一.

    If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

    service = build('sheets', 'v4', credentials=creds)
    
    spreadsheetId = "###"  # Please set Spreadsheet ID
    sheetId = "###"  # Please set sheet ID.
    
    body = {
      "requests": [
        {
          "updateCells": {
            "range": {
              "sheetId": sheetId,
              "startRowIndex": 0,
              "endRowIndex": 1,
              "startColumnIndex": 0,
              "endColumnIndex": 1
            },
            "rows": [
              {
                "values": [
                  {
                    "userEnteredFormat": {
                      "backgroundColor": {
                        "red": 1
                      }
                    }
                  }
                ]
              }
            ],
            "fields": "userEnteredFormat.backgroundColor"
          }
        }
      ]
    }
    res = service.spreadsheets().batchUpdate(spreadsheetId=spreadsheetId, body=body).execute()
    

    注意:

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