【发布时间】:2020-01-29 15:17:48
【问题描述】:
这就是我想要做的:
data = drive.files().get_media(fileId=fileId).execute()
csvData = [row.split(",") for row in str(data, 'latin-1').split("\n")]
ar = []
temp = []
for i, row in enumerate(csvData):
if "".join(row) != "":
temp.append(row)
else:
ar.append(temp)
temp = []
if i == len(csvData) - 1:
ar.append(temp)
# Create request body for the method of spreadsheets.create of Sheets API. [Tanaike]
sheetsObj = []
for sheet in ar:
tempRow = []
for row in sheet:
tempCol = []
for col in row:
tempCol.append({"userEnteredValue": {"stringValue": col}})
if len(tempCol) != 0:
tempRow.append({"values": tempCol})
if len(tempRow) != 0:
sheetsObj.append({"data": [{"rowData": tempRow}]})
# Request to Sheets API. [Tanaike]
body = {"properties": {"title": "spreadsheetTitle"}, "sheets": sheetsObj}
res = sheets.spreadsheets().create(body=body).execute()
print(res)
我最近问了一个关于通过 google API 将文件上传到 google drive 的问题。我在代码上取得了成功,但现在我意识到我需要一个单独的代码,而不是将数据上传到新文件中,而是更新现有的 google 表格。
我一直在与 batchUpdate() (https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/batchUpdate) 作斗争,因为我不知道如何将旧的 sheetObj 解决方案融入其中。
思路和这里一样:Google docs formatting, converting texts into sheets
我希望使用 .txt 格式的数据更新现有的 google 表格文件,但不是简单地上传并因此创建新文件,而是希望更新我目前拥有的现有 google 表格文件。
- 这个想法是覆盖现有文件中的任何内容。假设文件 id 是已知的。
ps.:发生了一些我之前没有注意到的奇怪事情:所有单元格都以 ' 开头,我正在尝试处理它,但如果有人在我做之前弄清楚了,请将其包含在解决方案中。
【问题讨论】:
标签: python google-drive-api google-sheets-api