So this is what i'm trying to do:
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)
I recently asked a question regarding an upload of files to google drive via google API. I had success with the code, but now I realized I need a separate code that, instead of uploading the data into a new file, updates an existing google sheets.
I've been struggling against batchUpdate() (https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/batchUpdate), because i don't know how to fit my old solution for sheetsObj into it.
The idea is the same as here: Google docs formatting, converting texts into sheets
I wish to update an existing google sheets file with data in .txt format, but instead of simply uploading and thus creating new files, I wish to update the existing google sheets files I currently have.
- The idea is to overwrite whatever is in the existing files. Assume that the file id is known.
ps.: There is something weird going on which I didn't notice earlier: all the cells start with a '
, I'm trying to deal with it, but if someone figures it out before I do, please include it in the solution.