在哪里添加 valueInputOption 以启用格式化日期? (google 张 API)

where to add the valueInputOption to enable formatting dates? (google sheets API)

我有一个 python 脚本,它必须将 google sheet 中的列更新为特定日期格式:

    req = [
          { 
            "repeatCell": {
              "range": {
                "sheetId": SHEET_ID,
                "startColumnIndex": 2,
                "endColumnIndex" : 3,
              },
              "cell": {
                "userEnteredFormat": {
                  "numberFormat": {
                      "type": "DATE",
                      "pattern": "dd-mmm-yyyy"
                },
                  
                  "horizontalAlignment" : "CENTER",  #OPTIONS: 'LEFT', 'RIGHT', 'CENTER'
                }
              },
              "fields": "userEnteredFormat(numberFormat,horizontalAlignment)",
            }
          }]
        body = {'requests':req}
        sheet.batchUpdate(spreadsheetId=SAMPLE_SPREADSHEET_ID, body=body).execute()

但是更新没有通过。我检查了一下,据我所知,这是因为文件中的日期格式是字符串(以 ' 开头,例如 '2021-03-06 14:19:17.102.

我看到一些答案建议我必须将 ValueInputOption 更改为 'USER_ENTERED',但我找不到在脚本中添加更改的位置?

例如:(How to format text as a number using the Google Sheets API?)

我尝试在最后一行添加 ValueInputOption='USER_ENTERED',我尝试了 body = {'requests':req, 'ValueInputOption':'USER_ENTERED'。不确定此更改应该去哪里?

或者是否有另一种方法可以将 'string' 覆盖为日期和数字格式?

解释:

ValueInputOption参数来自不同的batchUpdate请求,即spreadsheets.values.batchUpdate方法,不能在spreadsheets.batchUpdate.

中使用

此外,由于 ',单元格的值强制日期为字符串值,您需要手动或通过 spreadsheets.values.batchUpdate().

将其删除

参考文献:

spreadsheets.batchUpdate()

spreadsheets.values.batchUpdate()