Reputation: 1561
I am trying to fetch all values from a Google spreadsheet using gspread module.
I get an error
NameError: name 'worksheet' is not defined
Given below is what I am trying to do:
gc = gspread.authorize(credentials) . <<- Pass in Google sheet API to establish connection
list_of_lists = worksheet.get_all_values()
Could anyone assist. Thanks..
Upvotes: 0
Views: 4818
Reputation: 939
It looks like you're using the gspread library. From the documentation which is the same flow as the google API, there are steps you have to follow:
1. Authorize the client
client = pygsheets.authorize()
2. Select the file you want to open. gspread has three options:
2.a open using filename:
sh = gc.open('Your File name')
2.b open using sheet id(which you can get from the url after this part https://docs.google.com/spreadsheets/d/:
sht = gc.open_by_key('0BmgG6nO_6dprdS1MN3d3MkdPa142WFRrdnRRUWl1UFE')
2.c open using the entire url:
sht = gc.open_by_url('https://docs.google.com/spreadsheet/ccc?key=0Bm...FE&hl')
3. Select the sheet or tab.
Once its open, you can select the sheet or tab you want to get the data from
3.a Select worksheet by index. Worksheet indexes start from zero:
worksheet = sh.get_worksheet(0)
3.b by title:
worksheet = sh.worksheet("January")
3.c Or the most common case: Sheet1, assuming the sheet name doesn't have spaces:
worksheet = sh.sheet1
Note:
To get a list of all worksheets:
worksheet_list = sh.worksheets()
4. Extract data. Lastly to get all values of the sheet:
list_of_lists = worksheet.get_all_values()
So to summarize:
1. Authorize client
2. Select Spreadsheet
3. Select Sheet or Tab
4. Pull data
Link to the docs:
https://gspread.readthedocs.io/en/latest/
Upvotes: 1