Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_spreadsheet(self):
try:
return self.gspread_client.open_by_key(self.doc_id).worksheet(self.tab_id)
except gspread.exceptions.SpreadsheetNotFound as e:
raise Exception("Trying to open non-existent or inaccessible spreadsheet document.")
except gspread.exceptions.WorksheetNotFound as e:
raise Exception("Trying to open non-existent sheet. Verify that the sheet name exists (%s)." % self.tab_id)
except gspread.exceptions.APIError as e:
if hasattr(e, 'response'):
error_json = e.response.json()
print(error_json)
error_status = error_json.get("error", {}).get("status")
email = self.credentials.get("client_email", "(email missing)")
if error_status == 'PERMISSION_DENIED':
raise Exception("The Service Account does not have permission to read or write on the spreadsheet document. Have you shared the spreadsheet with %s?" % email)
if error_status == 'NOT_FOUND':
raise Exception("Trying to open non-existent spreadsheet document. Verify the document id exists (%s)." % self.doc_id)
raise Exception("The Google API returned an error: %s" % e)
self.__strip_empty_col()
except ValueError:
continue
value_matrix = self.__all_values[self._get_start_row_idx():]
try:
header_list = value_matrix[0]
record_list = value_matrix[1:]
except IndexError:
continue
self.inc_table_count()
yield TableData(
self.make_table_name(), header_list, record_list)
except gspread.exceptions.SpreadsheetNotFound:
raise OpenError("spreadsheet '{}' not found".format(self.title))
else:
open_func = self.client.open
try:
self.spread = open_func(spread)
self.refresh_spread_metadata()
except (SpreadsheetNotFound, NoValidUrlKeyFound, APIError) as error:
if create:
try:
self.spread = self.client.create(spread)
self.refresh_spread_metadata()
except Exception as e:
msg = "Couldn't create spreadsheet.\n" + str(e)
new_error = GspreadPandasException(msg)
elif isinstance(error, SpreadsheetNotFound) or "NOT_FOUND" in str(error):
new_error = SpreadsheetNotFound("Spreadsheet not found")
else:
new_error = error
# Raise new exception outside of except block for a python2/3 way to avoid
# "During handling of the above exception, another exception occurred"
if "new_error" in locals() and isinstance(new_error, Exception):
raise new_error
specified `title` is found.
>>> c = gspread.authorize(credentials)
>>> c.open('My fancy spreadsheet')
"""
try:
properties = finditem(
lambda x: x["name"] == title, self.list_spreadsheet_files(title)
)
# Drive uses different terminology
properties["title"] = properties["name"]
return Spreadsheet(self, properties)
except StopIteration:
raise SpreadsheetNotFound
open_func = self.client.open_by_url
else:
open_func = self.client.open
try:
self.spread = open_func(spread)
self.refresh_spread_metadata()
except (SpreadsheetNotFound, NoValidUrlKeyFound, APIError) as error:
if create:
try:
self.spread = self.client.create(spread)
self.refresh_spread_metadata()
except Exception as e:
msg = "Couldn't create spreadsheet.\n" + str(e)
new_error = GspreadPandasException(msg)
elif isinstance(error, SpreadsheetNotFound) or "NOT_FOUND" in str(error):
new_error = SpreadsheetNotFound("Spreadsheet not found")
else:
new_error = error
# Raise new exception outside of except block for a python2/3 way to avoid
# "During handling of the above exception, another exception occurred"
if "new_error" in locals() and isinstance(new_error, Exception):
raise new_error