def test_update(self): range_ = Range(1, 2, 1, 2, sheet="test") body = ValueRange(range_, Dimension.ROWS, values=[[3]]) response = self.repository.update(range_, body, ValueInputOption.RAW, include_values_in_response=True) self.assertEqual(body.get_str_values(), response.updated_data.values)
def test_get(self): range_ = Range(1, 1, 1, 1, sheet="test") expect = ValueRange(Range.init_from_str("test!A1"), Dimension.ROWS, values=[[1]]) actual = self.repository.get(range_=range_, major_dimension=Dimension.ROWS) self.assertEqual(expect.get_str_values(), actual.values) self.assertEqual(str(expect.range), str(actual.range)) self.assertEqual(expect.major_dimension, actual.major_dimension)
def append_purchase_data(self, buyer: str, item: str, cost: int): today = datetime.date.today() sheet_name = f"{today.year}/{today.month}" range_ = Range(1, 2, 4, 2, sheet=sheet_name) body = ValueRange(range_, Dimension.ROWS, values=[[str(today), buyer, item, cost]]) self.repository.append(range_, body, ValueInputOption.RAW)
def get( self, range_: Range, major_dimension=Dimension.DIMENSION_UNSPECIFIED, value_render_option=ValueRenderOption.FORMATTED_VALUE, date_time_render_option=DateTimeRenderOption.SERIAL_NUMBER ) -> ValueRange: service = build('sheets', 'v4', credentials=self.get_credentials()) request = service.spreadsheets().values().get( spreadsheetId=self.spreadsheet_id, range=range_, majorDimension=major_dimension.value, valueRenderOption=value_render_option.value, dateTimeRenderOption=date_time_render_option.value) response = request.execute() return ValueRange(Range.init_from_str(response["range"]), Dimension[response["majorDimension"]], response["values"])
def append( self, range_: Range, body: ValueRange, value_input_option: ValueInputOption, insert_data_option=InsertDataOption.OVERWRITE, include_values_in_response=False, response_value_render_option=ValueRenderOption.FORMATTED_VALUE, response_date_time_render_option=DateTimeRenderOption.SERIAL_NUMBER ) -> AppendResponse: service = build('sheets', 'v4', credentials=self.get_credentials()) request = service.spreadsheets().values().append( spreadsheetId=self.spreadsheet_id, range=str(range_), body={ "range": str(body.range), "majorDimension": body.major_dimension.value, "values": body.values }, valueInputOption=value_input_option.value, insertDataOption=insert_data_option.value, includeValuesInResponse=include_values_in_response, responseValueRenderOption=response_value_render_option.value, responseDateTimeRenderOption=response_date_time_render_option.value ) response = request.execute() updates = response["updates"] updated_data = None if ("updatedData" in updates): updated_data = updates["updatedData"] updated_data = ValueRange( range_=updated_data["range"], major_dimension=updated_data["majorDimension"], values=updated_data["values"]) return AppendResponse(spreadsheet_id=response["spreadsheetId"], table_range=response.get("tableRange"), updates=UpdateValuesResponse( spreadsheet_id=updates["spreadsheetId"], updated_range=updates["updatedRange"], updated_rows=updates["updatedRows"], updated_columns=updates["updatedColumns"], updated_cells=updates["updatedCells"], updated_data=updated_data))
def init_sheet(self, sheet: str): range_ = Range(1, 1, 4, 1, sheet=sheet) body = ValueRange(range_, Dimension.ROWS, values=[["date", "buyer", "item", "cost"]]) self.repository.update(range_, body, ValueInputOption.RAW)