简体   繁体   English

使用 gspread 提取工作表 ID

[英]Using gspread to extract sheet ID

Can't seem to find any answer to this, but are there any functions/methods which can get a worksheet ID?似乎找不到任何答案,但是是否有任何可以获取工作表 ID 的函数/方法?

Currently, my code looks like this:目前,我的代码如下所示:

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)

but I don't want to hardcode the abcdefg1234567abcdefg1234567 .但我不想硬编码abcdefg1234567abcdefg1234567 Is there anything I can do, like sheet.id() ?有什么我可以做的,比如sheet.id()

I believe your goal as follows.我相信你的目标如下。

  • In order to use import_csv , you want to retrieve the Spreadsheet ID from sheet = client.open(str(self.googleSheetFile)).worksheet(str(self.worksheet)) .为了使用import_csv ,您需要从sheet = client.open(str(self.googleSheetFile)).worksheet(str(self.worksheet))检索电子表格 ID。
  • You want to achieve this using gspread with python.您想使用 gspread 和 python 来实现这一点。

In this case, you can retrieve the Spreadsheet ID from client.open(str(self.googleSheetFile)) .在这种情况下,您可以从client.open(str(self.googleSheetFile))检索电子表格 ID。 So please modify your script as follows.因此,请按如下方式修改您的脚本。

From:从:

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

client.import_csv('abcdefg1234567abcdefg1234567', contents)

To:到:

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

Note:笔记:

  • When I saw the document of gspread, it says as follows.当我看到gspread的文档时,它是这样说的。 So please be careful this.所以请注意这一点。

    This method removes all other worksheets and then entirely replaces the contents of the first worksheet.此方法删除所有其他工作表,然后完全替换第一个工作表的内容。

  • This modified script supposes that you have already been able to get and put values for Google Spreadsheet using Sheets API with gspread.这个修改后的脚本假设您已经能够使用带有 gspread 的 Sheets API 获取和放置 Google 电子表格的值。

Reference:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM