我試圖使用gspread Python package從命令行將CSV數據導入到Google工作表中。什麼是gspread import_csv file_id參數?
Using this guide,我得到了一切工作,並能夠讀取和寫入單元格。
但是,更新單元格1對1太慢了,所以我現在試圖使用import_csv()
方法。 The docs say:
import_csv(的file_id,數據) 將數據導入到電子表格的第一頁。
參數:data - CSV字符串的數據。
file_id
在這裏沒有描述,我不能確定它應該是什麼。其他一些方法也使用file_id
併爲他們它被描述爲: - (又名文件ID)
file_id的電子表格ID
我不知道在哪裏找到電子表格ID,和無論我嘗試什麼,我都會遇到權限錯誤。由於我能夠使用update_cell()
,如上所述,我認爲我的權限工作正常,但使用的是錯誤的file_id
。
這裏的簡化代碼:
import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
client = gspread.authorize(creds)
sheet = client.open("SheetTitle").sheet1
# This works fine, so I think permissions etc are all set up correctly
sheet.update_cell(1, 1, 'Foo')
# Now try importing CSV data from a string
csv="""2016, 2017
1,2
3,4
"""
# Does not work
client.import_csv(sheet, csv);
# Using the spreadsheet_id in the URL as described here https://developers.google.com/sheets/api/guides/concepts#spreadsheet_id
client.import_csv('11x...', csv);
# Using the "#gid=0" value in the query string in the browser when looking at this sheet
client.import_csv(0, csv);
這裏的錯誤我得到的,無論哪個上面我嘗試:
Traceback (most recent call last):
File "./simple.py", line 22, in <module>
client.import_csv(sheet, csv);
File "/Library/Python/2.7/site-packages/gspread/client.py", line 297, in import_csv
headers=headers
File "/Library/Python/2.7/site-packages/gspread/httpsession.py", line 82, in put
return self.request('PUT', url, params=params, data=data, **kwargs)
File "/Library/Python/2.7/site-packages/gspread/httpsession.py", line 69, in request
response.status_code, response.content))
gspread.exceptions.RequestError: (403, '403: {\n "error": {\n "errors": [\n {\n "domain": "global",\n "reason": "insufficientPermissions",\n "message": "Insufficient Permission"\n }\n ],\n "code": 403,\n "message": "Insufficient Permission"\n }\n}\n')
工程驗證,如果ID是一樣的,你正在使用的輸入之一!謝謝!這與'11x ...'spreadsheet_id結合起來可以做到。我在文檔的某個地方錯過了這個嗎? –
這是記錄,但其他方法:https://gspread.readthedocs.io/en/latest/#gspread.Client.create –