在何处添加ValueInput选项以启用日期格式设置?(谷歌网页API)

2024-09-28 05:20:43 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

但是,更新不会通过。我检查了,据我所知,这是因为文件中的日期格式是string(以',比如'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'。不确定这个更改应该放在哪里

或者是否有其他方法将“字符串”改写为日期和数字格式


Tags: 脚本id格式googlebodyrequestsreqcenter
1条回答
网友
1楼 · 发布于 2024-09-28 05:20:43

说明:

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

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

参考文献:

spreadsheets.batchUpdate()

spreadsheets.values.batchUpdate()

相关问题 更多 >

    热门问题