2

I am having an issue with Gspread's get_all_values()

import json
import gspread
from oauth2client.client import SignedJwtAssertionCredentials
from gmail_variables import *

json_key = json.load(open('key7367.json'))
scope = ['https://spreadsheets.google.com/feeds']

credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'], scope)

gc = gspread.authorize(credentials)


wks = gc.open_by_url('https://docs.google.com/a/test.com/spreadsheets/d/1-cAg..sLt5Ho/edit?usp=sharing')

recipients = wks.get_all_values()

I am trying to write an email program to pull from a Google Spreadsheet. When I try to run it; I get the error.

'Spreadsheet' object has no attribute 'get_all_values'

Any help is appreciated.

jaysig
  • 71
  • 1
  • 2
  • 7

2 Answers2

1

You're just opening the workbook and not specifying the worksheet you want to get_all_values from. You need to call the get_worksheet() method. Try:

wks = gc.open_by_url('https://docs.google.com/a/test.com/spreadsheets/d/1-cAg..sLt5Ho/edit?usp=sharing').get_worksheet(0)

recipients = wks.get_all_values()

Where the '0' is the index of the worksheet (0 being the first worksheet in the workbook).

darksideofthesun
  • 601
  • 2
  • 9
  • 19
0

Once you open the spreadsheet(open_by_url), you need to specify the worksheet you are trying to work with, even if you only have one. You can do this a few different ways:

By index (starts from 0)

sh = wks.get_worksheet(0)

By title

sh = wks.worksheet("January")

Most common case: Sheet1

sh = wks.sheet1

So the fix would be

wks = gc.open_by_url('https://docs.google.com/a/test.com/spreadsheets/d/1-cAg..sLt5Ho/edit?usp=sharing')
sh = wks.get_worksheet(0)
recipients = sh.get_all_values()