def get_range(self, row, column, number_of_row=1, number_of_column=1): """ Get a range object for given cells coordinates. :param row: Starting row of the target range (int) :param column: Starting column of the target range (int) :param number_of_row: Number of rows in the target range. :param number_of_column: Number of columns in the target range. :return: Range object. """ if row == 0 or column == 0: raise RowOrColumnEqualsZeroError( """Row and column parameters can not be equal to 0. The cell A1 is row=1 and column=1""" ) a1 = convert_coordinates_to_a1( row, column, number_of_row, number_of_column, self.name ) return Range( client=self.client, sheet=self, a1=a1, )
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 get_cell( self, row, column): # todo: have a custom error when row and/or column is 0 row_number = self.coordinates.row + row - 1 column_number = self.coordinates.column + column - 1 a1 = convert_coordinates_to_a1(row_number, column_number, sheet_name=self.coordinates.sheet_name) return Range(client=self.client, sheet=self.sheet, a1=a1)
def offset(self, row_offset, column_offset, num_rows=None, num_columns=None): """ Returns a new range that is relative to the current range, whose upper left point is offset from the current range by the given rows and columns, and with the given height and width in cells. :param row_offset: The number of rows down from the range's top-left cell; negative values represent rows up from the range's top-left cell. :param column_offset: The number of columns right from the range's top-left cell; negative values represent columns left from the range's top-left cell. :param num_rows: (Optional) The height in rows of the new range. :param num_columns: (Optional) The width in columns of the new range. :return: A range object that corresponds to the new offset range """ top_row = self.coordinates.row if row_offset != 0: if row_offset < 0 and abs(row_offset) >= top_row: raise ValueError( "Tried creating an offset of " + str(row_offset) + " rows from the top of a range starting at row " + str(top_row) + ".") top_row = top_row + row_offset left_column = self.coordinates.column if column_offset != 0: if column_offset < 0 and abs(column_offset) >= left_column: raise ValueError( "Tried creating an offset of " + str(column_offset) + " columns to the left of a range starting at column " + str(left_column) + ".") left_column = left_column + column_offset number_of_rows = self.coordinates.number_of_rows if num_rows is not None: if num_rows <= 0: raise ValueError("Tried creating an offset of " + str(num_rows) + " rows.") number_of_rows = num_rows number_of_columns = self.coordinates.number_of_columns if num_columns is not None: if num_columns <= 0: raise ValueError("Tried creating an offset of " + str(num_columns) + " columns.") number_of_columns = num_columns new_range_a1 = convert_coordinates_to_a1( row=top_row, column=left_column, number_of_row=number_of_rows, number_of_column=number_of_columns, sheet_name=self.coordinates.sheet_name) return Range(client=self.client, sheet=self.sheet, a1=new_range_a1)
def get_cell(self, row, column): row_number = self.coordinates.row + row - 1 column_number = self.coordinates.column + column - 1 a1 = convert_coordinates_to_a1( row_number, column_number, sheet_name=self.coordinates.sheet_name ) return Range( client=self.client, sheet=self.sheet, a1=a1 )
def get_range(self): a1 = convert_coordinates_to_a1( row= self.table.items_range.coordinates.row + self.row_index, column=self.table.items_range.coordinates.column, number_of_row=1, number_of_column=self.table.items_range.coordinates.number_of_columns, sheet_name=self.table.items_range.coordinates.sheet_name ) return Range( client=self.table.full_range.client, sheet=self.table.full_range.sheet, a1=a1 )
def get_range(self, row, column, number_of_row=1, number_of_column=1): """ Get a range object for given cells coordinates. :param row: Starting row of the target range (int) :param column: Starting column of the target range (int) :param number_of_row: Number of rows in the target range. :param number_of_column: Number of columns in the target range. :return: Range object. """ a1 = convert_coordinates_to_a1(row, column, number_of_row, number_of_column) return Range( client=self.client, sheet=self, a1=a1, )