Ell*_*t B 5 python google-sheets-api
我已经在 Python 中使用这个 Google Sheets API 代码几个月来将数据附加到电子表格中,但它最近坏了。谁能看出这有什么问题?
body = {'values': [['2019-9-1', '41']]}
result = service.spreadsheets().values().append(spreadsheetId=SPREADSHEET_ID,valueInputOption='RAW', body=body, range='A:Z').execute()
Run Code Online (Sandbox Code Playgroud)
它创建这样的单元格,在每个单元格值前加上一个撇号:
+----+-----------+-----+
| | A | B |
+----+-----------+-----+
| 33 | '2019-9-1 | '41 |
+----+-----------+-----+
Run Code Online (Sandbox Code Playgroud)
这是结果:
{'spreadsheetId': 'xxxxxxxxx', 'tableRange': 'data!A1:U32', 'updates': {'spreadsheetId': 'xxxxxxxxxx', 'updatedRange': 'data!A33:B33', 'updatedRows': 1, 'updatedColumns': 2, 'updatedCells': 2}}
Run Code Online (Sandbox Code Playgroud)
Tan*_*ike 11
2019-9-1,并41以电子表格分别日期对象和数量。
'2019-9-1和'41。如果我的理解是正确的,这个修改怎么样?
我认为您的问题的原因是由于valueInputOption='RAW'. 当RAW设置为 时valueInputOption,官方文档是这样说的。
用户输入的值不会被解析,而是按原样存储。
由此,2019-9-1和41被放置为字符串类型。所以'被添加到字符的顶部。
为了把值2019-9-1和41作为Date对象和数量,请修改如下。
result = service.spreadsheets().values().append(spreadsheetId=SPREADSHEET_ID,valueInputOption='RAW', body=body, range='A:Z').execute()
Run Code Online (Sandbox Code Playgroud)
到:
result = service.spreadsheets().values().append(spreadsheetId=SPREADSHEET_ID,valueInputOption='USER_ENTERED', body=body, range='A:Z').execute()
Run Code Online (Sandbox Code Playgroud)
当USER_ENTERED设置为 时valueInputOption,官方文档是这样说的。
这些值将被解析,就像用户在 UI 中键入它们一样。数字将保留为数字,但字符串可能会转换为数字、日期等,遵循通过 Google 表格 UI 在单元格中输入文本时应用的相同规则。
如果我误解了您的问题并且这不是您想要的结果,我深表歉意。