【问题标题】:How do I access different tabs in a Google Sheets via the Google Sheets API?如何通过 Google Sheets API 访问 Google Sheets 中的不同选项卡?
【发布时间】:2019-07-31 23:36:51
【问题描述】:

我正在尝试使用 Google Drive/Sheets API 从 Google Drive 读取工作表。我想知道如何在不同的标签之间导航。

(忽略其他的东西,这是一张展示标签是什么的随机图片。)

基本上,我想要做的是能够访问其他选项卡并阅读那里的文本。每个选项卡的格式相同。我已经能够从第一个选项卡中读取内容,并且也希望对其他选项卡执行相同的操作。

这是我的代码:

import gspread
from oauth2client.service_account import ServiceAccountCredentials
import pprint


# Use credentials to interact with the Google Drive API
scope = ['https://spreadsheets.google.com/feeds',
         'https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name(
    'foo', scope)
client = gspread.authorize(credentials)

# Opening the first sheet
sheet = client.open("Resume Data").sheet1

# Prints the code in a nicer format
pp = pprint.PrettyPrinter()


# Extract and print all of the values
list_of_resumes = sheet.get_all_records()

# Printing
pp.pprint(list_of_resumes)

【问题讨论】:

  • 关于I've been able to read from the first tab,你能提供你的脚本吗?如果可以,请将其添加到您的问题中。通过这种方式,我们可以了解您访问电子表格的方法。当然,请删除您的个人信息。
  • @Tanaike 我现在已经做到了。谢谢!
  • 感谢您的回复和补充信息。我提出了一个修改后的脚本作为答案。你能确认一下吗?如果我误解了您的问题并且这不是您想要的结果,我深表歉意。

标签: python google-drive-api google-sheets-api


【解决方案1】:
  • 您希望从除了第一个索引的工作表之外的工作表中获取值。
  • 您希望使用 gspread 实现此目的。
  • 您已经能够为电子表格获取和输入值。

如果我的理解是正确的,那么这个修改呢?

发件人:

sheet = client.open("Resume Data").sheet1

收件人:

spreadsheetName = "Resume Data"
sheetName = "###"  # <--- please set the sheet name here.
spreadsheet = client.open(spreadsheetName)
sheet = spreadsheet.worksheet(sheetName)

注意:

  • 如果要使用索引选择工作表,请使用sheet = spreadsheet.get_worksheet(1)。这意味着选择第二个索引的工作表。

参考:

【讨论】:

  • @Atharva Gupta 很高兴您的问题得到了解决。感谢您的回复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-17
  • 2019-07-15
  • 1970-01-01
  • 2018-10-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多