Usecase: I am iterating through rows in a Google sheet and taking some actions dependant on the value in different fields.
client = authorize()
worksheets_data = get_worksheet(client, CAREEM_SHEET, "UAE")
sheet_data = SheetData(worksheets_data)
rows = sheet_data.get_all_values()
for row in rows[2:]: # Skip header and template row
channel_link_id = row[sheet_data.get_column_index(ActivationSheets.CAREEM_CHANNEL_LINK_ID.value)]
careem_mapped = row[sheet_data.get_column_index(ActivationSheets.CAREEM_MAPPED.value)]
menu_pushed = row[sheet_data.get_column_index(ActivationSheets.CAREEM_MENU_PUSHED.value)]
created = row[sheet_data.get_column_index(ActivationSheets.CAREEM_DRECT_CREATED.value)]
if (careem_mapped == "TRUE" and menu_pushed != "Yes" and created == "TRUE"):
print(row)
Issue: menu_pushed is a drop down in the sheet using data validation. This does not seem to pick up any values using the get_all_values() function.
Do you know how I can access the data validation from a cell? All I can seem to find in the docs is set_data_validation()
Example output from print statement -
['6093be05481dce29eaac6bfe', 'TRUE', 'FALSE', '1/23/2022 13:45:47', '']
My SheetData class -
class SheetData:
def __init__(self, worksheet: pygsheets.Worksheet):
self._worksheet = worksheet
self._header_indexes = {}
self._all_values = None
self._populate_fields()
def _populate_fields(self):
self._all_values = self._worksheet.get_all_values()
headers = self._all_values[0]
self._header_indexes = {header: index for index, header in enumerate(headers)}
@property
def header_indexes(self):
return self._header_indexes
def get_all_values(self):
"""
:return: all cell values from within the supplied worksheet.
"""
return self._worksheet.get_all_values()
def authorize() -> pygsheets.client.Client:
"""
Authorizes the pygsheets client using a service account credentials file.
:param credentials_file: str location of Google Service Account credentials file.
:returns: A pygsheets.Client object.
:raises pygsheets.exceptions.PyGsheetsException: If the authorization process fails.
"""
return pygsheets.authorize(service_account_json=SPREADSHEET_SERVICE_ACCOUNT)
def get_worksheet(client: pygsheets.client.Client, spreadsheet_url: str, worksheet_name: str) -> pygsheets.Worksheet:
"""
Fetches a worksheet from a Google Sheets spreadsheet.
:param client: pygsheets.Client object.
:param spreadsheet_url: str URL of the spreadsheet.
:param worksheet_name: str name of the worksheet to retrieve.
:returns: A pygsheets.Worksheet object representing the specified worksheet.
:raises pygsheets.exceptions.PyGsheetsException: If the worksheet retrieval fails.
"""
spreadsheet = client.open_by_url(spreadsheet_url)
return spreadsheet.worksheet_by_title(worksheet_name)
Resolved - This was caused by a Google Apps Script attached to this specific spreadsheet, that was modifying the sheet and changing data validation dynamically.
Removed the unnecessary script and it worked perfectly.