Bruuuh
Bruuuh

Reputation: 133

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
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()

What I tried :

updateCells body

        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"
                    }
                }
            ]
        }

Any tips and tricks or ideas are welcome !

Upvotes: 2

Views: 1500

Answers (1)

Tanaike
Tanaike

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?

For 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'
            }
        }
    ]
}

For 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"
            }
        }
    ]
}

Note:

  • In above modified request body, for example, when the values of rown and coln are 1 and 1, respectively, the number format of cell "A1" is modified.
  • In this modified script, it supposes that the value of 09/11/2021 in the cell is the date object. Please be careful this.

Reference:

Upvotes: 3

Related Questions