簡體   English   中英

使用 gspread 提取工作表 ID

[英]Using gspread to extract sheet ID

似乎找不到任何答案,但是是否有任何可以獲取工作表 ID 的函數/方法?

目前,我的代碼如下所示:

scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']

        ....code to authorize credentials goes here....

        sheet = client.open(str(self.googleSheetFile)).worksheet(str(self.worksheet))

        client.import_csv('abcdefg1234567abcdefg1234567', contents)

但我不想硬編碼abcdefg1234567abcdefg1234567 有什么我可以做的,比如sheet.id()

我相信你的目標如下。

  • 為了使用import_csv ,您需要從sheet = client.open(str(self.googleSheetFile)).worksheet(str(self.worksheet))檢索電子表格 ID。
  • 您想使用 gspread 和 python 來實現這一點。

在這種情況下,您可以從client.open(str(self.googleSheetFile))檢索電子表格 ID。 因此,請按如下方式修改您的腳本。

從:

sheet = client.open(str(self.googleSheetFile)).worksheet(str(self.worksheet))

client.import_csv('abcdefg1234567abcdefg1234567', contents)

到:

spreadsheet = client.open(str(self.googleSheetFile))
sheet = spreadsheet.worksheet(str(self.worksheet))
client.import_csv(spreadsheet.id, contents)

筆記:

  • 當我看到gspread的文檔時,它是這樣說的。 所以請注意這一點。

    此方法刪除所有其他工作表,然后完全替換第一個工作表的內容。

  • 這個修改后的腳本假設您已經能夠使用帶有 gspread 的 Sheets API 獲取和放置 Google 電子表格的值。

參考:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM