使用python更新Google电子表格的单元格背景颜色 [英] Update cell background color for google spreadsheet using python
问题描述
我想使用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()
注意:
- 在这种情况下,必须使用
Note:
- In this case, the range is required to be written with GridRange.
startRowIndex: 0, endRowIndex: 1m startColumnIndex: 0, endColumnIndex: 1
means the cell "A1".- spreadsheets.batchUpdate
- UpdateCellsRequest
- CellFormat
- GridRange
- Python快速入门
- 关于授权脚本,您可以在Python快速入门中看到它.
- spreadsheets.batchUpdate
- UpdateCellsRequest
- CellFormat
- GridRange
- Python Quickstart
- About the script for authorizing, you can see it at Python Quickstart.
如果我误解了您的问题,而这不是您想要的方向,我深表歉意.
If I misunderstood your question and this was not the direction you want, I apologize.
这篇关于使用python更新Google电子表格的单元格背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- In this case, the range is required to be written with GridRange.