使用 google sheet API V4 中的 python 将特定单元格格式化为日期
Format specific cell to date with python in google sheet API V4
我尝试将 google 电子表格中的一些单元格格式化为日期格式。
单元格上的数据如下所示:09/11/2021
我试着让它看起来像这样:11/2021
或者至少用日期格式格式化单元格。
我使用 batchUpdate() 来更新单元格:
request = service.spreadsheets().batchUpdate(spreadsheetId=spreadsheet_id, body=body).execute()
我尝试了什么:
- updateCells 和 repeatCell 但 none 都有效。
- 在将值添加到单元格之前和之后设置格式。
- 通过更改 rown 或 coln 值来更改单元格(在下面的代码中)
- 其他日期格式,例如
dd-mm-yy
或 mm yyyy
- 还阅读了大量 google 文档并观看了他们的视频...
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'
}
}
]
}
repeatCell body
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"
}
}
]
}
欢迎任何提示和技巧或想法!
我认为在您的请求正文中,您出现问题的原因可能是由于 'startColumnIndex': coln,
和 'endColumnIndex': coln
的值相同。那么,例如,按如下方式修改您的请求主体怎么样?
对于updateCells body
:
body = {
"requests": [
{
'updateCells': {
'range': {
'sheetId': sheet_id,
'startRowIndex': rown-1,
'endRowIndex': rown,
'startColumnIndex': coln-1, # Modified
'endColumnIndex': coln
},
'rows': {'values': [{'userEnteredFormat': {'numberFormat': {'type': "DATE", 'pattern': "mm/yyyy"}}}]},
'fields': 'userEnteredFormat.numberFormat'
}
}
]
}
对于repeatCell body
:
body = {
"requests": [
{
"repeatCell": {
"range": {
"sheetId": sheet_id,
"startRowIndex": rown-1,
"endRowIndex": rown,
"startColumnIndex": coln-1, # Modified
"endColumnIndex": coln
},
"cell": {
"userEnteredFormat": {
"numberFormat": {
"type": "DATE",
"pattern": "mm/yyyy"
}
}
},
"fields": "userEnteredFormat.numberFormat"
}
}
]
}
注:
- 在上面修改的请求体中,例如当
rown
和coln
的值分别为1
和1
时,单元格的数字格式“ A1”被修改。
- 在这个修改后的脚本中,假设单元格中
09/11/2021
的值是日期对象。请注意这一点。
参考:
我尝试将 google 电子表格中的一些单元格格式化为日期格式。
单元格上的数据如下所示:09/11/2021
我试着让它看起来像这样:11/2021
或者至少用日期格式格式化单元格。
我使用 batchUpdate() 来更新单元格:
request = service.spreadsheets().batchUpdate(spreadsheetId=spreadsheet_id, body=body).execute()
我尝试了什么:
- updateCells 和 repeatCell 但 none 都有效。
- 在将值添加到单元格之前和之后设置格式。
- 通过更改 rown 或 coln 值来更改单元格(在下面的代码中)
- 其他日期格式,例如
dd-mm-yy
或mm yyyy
- 还阅读了大量 google 文档并观看了他们的视频...
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'
}
}
]
}
repeatCell body
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"
}
}
]
}
欢迎任何提示和技巧或想法!
我认为在您的请求正文中,您出现问题的原因可能是由于 'startColumnIndex': coln,
和 'endColumnIndex': coln
的值相同。那么,例如,按如下方式修改您的请求主体怎么样?
对于updateCells body
:
body = {
"requests": [
{
'updateCells': {
'range': {
'sheetId': sheet_id,
'startRowIndex': rown-1,
'endRowIndex': rown,
'startColumnIndex': coln-1, # Modified
'endColumnIndex': coln
},
'rows': {'values': [{'userEnteredFormat': {'numberFormat': {'type': "DATE", 'pattern': "mm/yyyy"}}}]},
'fields': 'userEnteredFormat.numberFormat'
}
}
]
}
对于repeatCell body
:
body = {
"requests": [
{
"repeatCell": {
"range": {
"sheetId": sheet_id,
"startRowIndex": rown-1,
"endRowIndex": rown,
"startColumnIndex": coln-1, # Modified
"endColumnIndex": coln
},
"cell": {
"userEnteredFormat": {
"numberFormat": {
"type": "DATE",
"pattern": "mm/yyyy"
}
}
},
"fields": "userEnteredFormat.numberFormat"
}
}
]
}
注:
- 在上面修改的请求体中,例如当
rown
和coln
的值分别为1
和1
时,单元格的数字格式“ A1”被修改。 - 在这个修改后的脚本中,假设单元格中
09/11/2021
的值是日期对象。请注意这一点。