def persist_a1_data_range(self, a1): """ If a1 attribute is None (typically when we get_data_range, it calculates the a1 notation and range coordinates based on the number of rows and columns found in the data. :param data: raw response of a request for values to google sheets API. """ if a1 is not None: return a1 request = self.client.sheet_service.spreadsheets().values().get( spreadsheetId=self.sheet.spreadsheet.id, range=self.sheet.name) response = request.execute() display_a1 = response["range"] first_cell_a1 = display_a1.split('!')[1].split(':')[0] if response.get("values") is None: raise NoDataRangeError('No data found in sheet "{}"'.format( self.sheet.name)) number_of_rows = len(response["values"]) number_of_columns = 0 for row in response["values"]: if len(row) > number_of_columns: number_of_columns = len(row) first_cell_coordinates = convert_a1_to_coordinates(first_cell_a1) return convert_coordinates_to_a1(row=first_cell_coordinates.row, column=first_cell_coordinates.column, number_of_row=number_of_rows, number_of_column=number_of_columns, sheet_name=self.sheet.name)
def __init__(self, client, sheet, a1=None): """ Object to represent a range of cells in a sheet. :param client: :param sheet: parent Sheet object. :param a1: """ self.client = client self.sheet = sheet # this checks the data range coordinates self.a1 = self.persist_a1_data_range(a1) self.coordinates = convert_a1_to_coordinates(self.a1) # placeholder for putting requests object if we # want to send multiple set requests in one api call. self.batches = list()