使用 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 正文

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

注:

  • 在上面修改的请求体中,例如当rowncoln的值分别为11时,单元格的数字格式“ A1”被修改。
  • 在这个修改后的脚本中,假设单元格中09/11/2021的值是日期对象。请注意这一点。

参考: