Reputation: 133
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
And I try to make it looks like this : 11/2021
Or at least format the cell with a date format.
I use batchUpdate() to update cells :
request = service.spreadsheets().batchUpdate(spreadsheetId=spreadsheet_id, body=body).execute()
dd-mm-yy
or 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 !
Upvotes: 2
Views: 1500
Reputation: 201378
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. So, for example, how about modifying your request bodies as follows?
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
and coln
are 1
and 1
, respectively, the number format of cell "A1" is modified.09/11/2021
in the cell is the date object. Please be careful this.Upvotes: 3