【发布时间】:2020-10-19 06:47:56
【问题描述】:
与这个问题(Update Google spreadsheet cell (python))相关,我想知道如何更改链接示例中单元格(或整行)的背景颜色。
这是我更新工作表的代码部分。我通过服务和行。当我只在一个带有 'values' 变量的单元格中写 'OK' 时,它起作用了,而不是对行背景的修改:
SPREADSHEET_ID = '###'
WORKSHEET_NAME = 'Name of the sheet'
async def escribirEnSheet(service, row):
# print('voy a escribir en celda ' + str(range_))
# range_ = 'M'+str(row) # TODO: Update placeholder value.
row += 1
range_ = WORKSHEET_NAME + "!M" + str(row)
print('voy a escribir en celda: ' + str(range_))
#values = [['OK']]
sheetObj = service.spreadsheets().get(spreadsheetId=SPREADSHEET_ID, fields='sheets(properties(sheetId,title))').execute()
sheet_id = ""
for sheet in sheetObj['sheets']:
if sheet['properties']['title'] == WORKSHEET_NAME:
sheet_id = sheet['properties']['sheetId']
break
batch_update_spreadsheet_request_body = {
"requests": [
{
"updateCells": {
"range": {
"sheetId": sheet_id,
"startRowIndex": row,
"endRowIndex": row+1,
"startColumnIndex": 12,
"endColumnIndex": 12
},
"rows": [
{
"values": [
{
"userEnteredValue": {
"stringValue": "OK"
}
}
]
}
],
"fields": "userEnteredValue"
}
},
{
"repeatCell": {
"range": {
"sheetId": WORKSHEET_NAME,
"startRowIndex": row,
"endRowIndex": row+1,
"startColumnIndex": 0,
},
"cell": {
"userEnteredFormat": {
"backgroundColor": {
"red": 0,
"green": 1,
"blue": 0
}
}
},
"fields": "userEnteredFormat.backgroundColor"
}
}
]
}
value_input_option = 'RAW' # TODO: Update placeholder value.
try:
print('voy a hacer la request para escribir')
request = service.spreadsheets().batchUpdate(spreadsheetId=SPREADSHEET_ID, body=batch_update_spreadsheet_request_body)
response = request.execute()
time.sleep(1)
print('ya he escrito')
print(response)
except:
print('algo ha ocurrido al escribir en la sheet')
traceback.print_exc()
pass
提前致谢!
【问题讨论】:
标签: python google-sheets background-color google-sheets-api