【发布时间】:2021-04-07 17:35:24
【问题描述】:
我尝试在谷歌电子表格中将一些单元格格式化为日期格式。
单元格上的数据如下所示:09/11/2021
我试着让它看起来像这样:11/2021
或者至少用日期格式格式化单元格。
我使用 batchUpdate() 更新单元格:request = service.spreadsheets().batchUpdate(spreadsheetId=spreadsheet_id, body=body).execute()
我尝试了什么:
- updateCells 和 repeatCell 均无效。
- 将值添加到单元格之后,在 AND 之前进行格式化。
- 通过更改 rown 或 coln 值(在下面的代码中)来更改单元格
- 其他日期格式,如
dd-mm-yy或mm yyyy - 还阅读了大量谷歌文档并观看了他们的视频...
updateCells 正文
body = {
"requests": [
{
'updateCells': {
'range': {
'sheetId': sheet_id,
'startRowIndex': rown-1,
'endRowIndex': rown,
'startColumnIndex': coln,
'endColumnIndex': coln
},
'rows': {'values': [{'userEnteredFormat': {'numberFormat': {'type': "DATE", 'pattern': "mm/yyyy"}}}]},
'fields': 'userEnteredFormat.numberFormat'
}
}
]
}
重复细胞体
body = {
"requests": [
{
"repeatCell": {
"range": {
"sheetId": sheet_id,
"startRowIndex": rown-1,
"endRowIndex": rown,
"startColumnIndex": coln,
"endColumnIndex": coln
},
"cell": {
"userEnteredFormat": {
"numberFormat": {
"type": "DATE",
"pattern": "mm/yyyy"
}
}
},
"fields": "userEnteredFormat.numberFormat"
}
}
]
}
欢迎任何提示和技巧或想法!
【问题讨论】:
-
在batchUpdate()中使用json.dumps()之前是否尝试过使用body?
标签: python-3.x google-sheets formatting google-sheets-api