我正试图使用gspread模块从谷歌电子表格中获取所有值。
我收到一个错误
NameError: name 'worksheet' is not defined
以下是我正在尝试做的事情:
gc = gspread.authorize(credentials) . <<- Pass in Google sheet API to establish connection
list_of_lists = worksheet.get_all_values()
有人能帮忙吗。谢谢
看起来您正在使用gspread库。从与google API相同流程的文档中,您必须遵循以下步骤:
1授权客户端client = pygsheets.authorize()
2选择要打开的文件。gspread有三个选项:
nbsp;2.使用文件名打开:
nbsp nbsp;sh = gc.open('Your File name')
nbsp;2.b使用工作表id打开(您可以从本部分后面的url中获得https://docs.google.com/spreadsheets/d/:
nbsp nbsp;sht = gc.open_by_key('0BmgG6nO_6dprdS1MN3d3MkdPa142WFRrdnRRUWl1UFE')
nbsp;2.c使用整个url打开:
nbsp nbsp;sht = gc.open_by_url('https://docs.google.com/spreadsheet/ccc?key=0Bm...FE&hl')
3选择工作表或选项卡。打开后,您可以选择要从中获取数据的工作表或选项卡
nbsp;3.a按索引选择工作表。工作表索引从零开始:
nbsp nbsp;worksheet = sh.get_worksheet(0)
nbsp;3.b按标题:
nbsp nbsp;worksheet = sh.worksheet("January")
nbsp;3.c或者最常见的情况:Sheet1,假设工作表名称没有空格:
nbsp nbsp;worksheet = sh.sheet1
注意:
若要获取所有工作表的列表:worksheet_list = sh.worksheets()
4提取数据。最后要获取工作表的所有值:list_of_lists = worksheet.get_all_values()
所以总结一下:
1。授权客户端
2。选择电子表格
3。选择工作表或选项卡
4。提取数据
文档链接:
https://gspread.readthedocs.io/en/latest/