python - 如何通过 Google Sheets API 访问 Google Sheets 中的不同选项卡?

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

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

The tab is the circled part, (ignore the other stuff)
(来源: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/

相关文章:

python - 链表,如果 __init__() 中参数中的元素是可迭代的,则从中构造新的链表

python - subprocess.check_output() 似乎不存在(Python 2.6.5)

google-drive-api - 谷歌驱动器 maxResults 查询

ruby - Google Sheets v4 API Ruby - 尝试更新列中单元格中的值,但更新没有发生

python - 在 View 中使用的设置中添加变量 - Django

python - 在 Python 中执行此列表任务的最短/最佳方法

python - 使用刷新 token 刷新谷歌访问 token

IOS GoogleDrive API - 通过 1 次执行获取子文件夹中的所有文件

c# - 如何将 IList<object> 转换为字符串数组?

angular - Google API 回调更改后 View 未更新