我正在尝试使用 Google Drive/Sheets API 从 Google Drive 读取工作表。我想知道如何在不同的选项卡之间导航。
(来源:cachefly.net)
(忽略其他内容,这是一张随机图片,演示了选项卡是什么。)
本质上,我想做的是能够访问其他选项卡并阅读那里的文本。每个选项卡都采用相同的格式。我已经能够从第一个选项卡中读取内容,并且也希望对其他选项卡执行相同的操作。
这是我的代码:
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)
最佳答案
- 您想要从除第一个索引的工作表之外的工作表中获取值。
- 您想使用 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 =电子表格.get_worksheet(1)
。这意味着选择第二个索引的工作表。
引用:
关于python - 如何通过 Google Sheets API 访问 Google Sheets 中的不同选项卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57300096/