[英]Format specific cell to date with python in google sheet API V4
I try to format some cells to a date format in a google spreadsheet.我尝试将一些单元格格式化为谷歌电子表格中的日期格式。
The data on the cell looks like this: 09/11/2021单元格上的数据如下所示: 09/11/2021
And I try to make it looks like this: 11/2021我试着让它看起来像这样: 11/2021
Or at least format the cell with a date format.或者至少用日期格式格式化单元格。
I use batchUpdate() to update cells:我使用batchUpdate()更新单元格:
request = service.spreadsheets().batchUpdate(spreadsheetId=spreadsheet_id, body=body).execute()
dd-mm-yy
or mm yyyy
其他日期格式,如dd-mm-yy
或mm yyyy
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"
}
}
]
}
Any tips and tricks or ideas are welcome !欢迎任何提示和技巧或想法!
I thought that in your request body, the reason of your issue might be due to that the values of 'startColumnIndex': coln,
and 'endColumnIndex': coln
are the same.我认为在您的请求正文中,您的问题的原因可能是由于'startColumnIndex': coln,
和'endColumnIndex': coln
的值相同。 So, for example, how about modifying your request bodies as follows?那么,例如,如何修改您的请求正文如下?
updateCells body
:对于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
:对于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
and coln
are 1
and 1
, respectively, the number format of cell "A1" is modified.在上述修改后的请求体中,例如,当rown
和coln
的值分别为1
和1
时,修改单元格“A1”的数字格式。09/11/2021
in the cell is the date object.在这个修改后的脚本中,假设单元格中09/11/2021
的值是日期 object。 Please be careful this.请注意这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.