亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何更新谷歌驅動器文件

如何更新谷歌驅動器文件

慕田峪9158850 2022-07-12 17:48:11
所以這就是我想要做的: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)我最近問了一個關于通過谷歌 API 將文件上傳到谷歌驅動器的問題。我在代碼上取得了成功,但現在我意識到我需要一個單獨的代碼,而不是將數據上傳到新文件中,而是更新現有的谷歌表格。我一直在努力反對 batchUpdate() (https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/batchUpdate),因為我不知道如何將舊的 sheetObj 解決方案融入它。這個想法和這里一樣:谷歌文檔格式,將文本轉換為表格我希望使用 .txt 格式的數據更新現有的 google 表格文件,但不是簡單地上傳并因此創建新文件,而是希望更新我目前擁有的現有 google 表格文件。這個想法是覆蓋現有文件中的任何內容。假設文件 id 是已知的。ps.:發生了一些我之前沒有注意到的奇怪事情:所有單元格都以 a 開頭',我正在嘗試處理它,但是如果有人在我做之前弄清楚了,請將其包含在解決方案中。
查看完整描述

1 回答

?
aluckdog

TA貢獻1847條經驗 獲得超7個贊

您希望使用來自以下值的 CSV 數據更新現有電子表格。這些值來自谷歌文檔格式,將文本轉換為表格


ID, NAME, MOBILE, CITY, COUNTRY, BIRTHDAY, 

3, NameGoesHere1, 21 98658 5548, abcity, countryNameHere, 1998-05-02, 

6, SomeoneElse Joined Here, 21 98535 1218, whereland, Far far away, 1989-11-15, 

5, AnotherCustomer, 21 85482 5245, somecity, Somewhereland, 1999-08-04, 


ID, PRICE, STOCK, ASDF, BASDF, CASDF,


ID, NAME, PRICE, DESCRIPTION, 

2, pen, 1.5, The pen is mightier than the sword, 

3, pencil, 1.0, Can be used to write, 

4, RPG, 150.0, well that escalated quickly, huh, 


EMPTY, 

names, 

goofs, 


ID, FLAVOR, 

  • 現有的電子表格有 5 張表格,用于使用 5 張表格的 CSV 數據進行更新。

  • 您想使用 google-api-python-client 和 python 來實現這一點。

  • 您已經能夠使用 Sheets API 獲取和放置電子表格的值。

如果我的理解是正確的,這個答案怎么樣?請認為這只是幾個可能的答案之一。

流動:

  1. 從 CSV 文件中檢索值。

  2. 解析每個工作表的值,并創建請求正文。

  3. 使用創建的請求正文請求現有電子表格。

    • 在這種情況下,我使用了電子表格.values.batchUpdate 的方法。

修改后的腳本:

在運行腳本之前,請設置csvFileId,spreadsheetId和的變量sheetNames。

csvFileId = '###'  # Please set the CSV file ID.

spreadsheetId = '###'  # Please set the Spreadsheet ID.

sheetNames = ['Sheet1', 'Sheet2', 'Sheet3', 'Sheet4', 'Sheet5']  # Please set the sheet names in the Spreadsheet for updating.


sheets = build('sheets', 'v4', credentials=creds)

drive = build('drive', 'v3', credentials=creds)


# Retrieve data from Google Drive and parse data as an array.

data = drive.files().get_media(fileId=csvFileId).execute()

csvData = [row.split(",") for row in str(data, 'utf-8').split("\n")]

ar = []

temp = []

for i, row in enumerate(csvData):

    if "".join(row) != "":

        row = [v.strip() for v in row]

        temp.append(row)

    else:

        ar.append(temp)

        temp = []

    if i == len(csvData) - 1:

        ar.append(temp)


valuesUpdateReq = []

for i, sheet in enumerate(ar):

    if bool(sheet):

        sheetName = sheetNames[i]

        valuesUpdateReq.append({"values": sheet, "range": sheetName, "majorDimension": "ROWS"})


# Request to Sheets API.

batch_update_values_request_body = {"data": valuesUpdateReq, "valueInputOption": "USER_ENTERED"}

res = sheets.spreadsheets().values().batchUpdate(spreadsheetId=spreadsheetId, body=batch_update_values_request_body).execute()

print(res)

筆記:

在上面的腳本中,這些值使用“USER_ENTERED”放入電子表格。這樣,這些值可以被解析為字符串、數字和日期。而且,不使用字符頂部的單引號。


查看完整回答
反對 回復 2022-07-12
  • 1 回答
  • 0 關注
  • 124 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號