From 94c7b7cc68dac8e7d6e7ab00e4983cbb2b2be113 Mon Sep 17 00:00:00 2001 From: Kordishal Date: Wed, 6 Jun 2018 09:21:26 +0200 Subject: [PATCH] [WIP] Moved sh_append to sheet API. --- pygsheets/client.py | 10 +--------- pygsheets/sheet.py | 19 +++++++++++++++++-- pygsheets/worksheet.py | 15 +++++++++------ 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/pygsheets/client.py b/pygsheets/client.py index f473321..a288c9f 100644 --- a/pygsheets/client.py +++ b/pygsheets/client.py @@ -269,15 +269,7 @@ def sh_copy_worksheet(self, src_ssheet, src_worksheet, dst_ssheet): return self._execute_request(dst_ssheet, final_request, False) def sh_append(self, spreadsheet_id, body, rranage, replace=False, batch=False): - """wrapper around batch append""" - if replace: - inoption = "OVERWRITE" - else: - inoption = "INSERT_ROWS" - final_request = self.service.spreadsheets().values().append(spreadsheetId=spreadsheet_id, range=rranage, body=body, - insertDataOption=inoption, includeValuesInResponse=False, - valueInputOption="USER_ENTERED") - self._execute_request(spreadsheet_id, final_request, batch) + """""" # @TODO use batch update more efficiently def sh_batch_update(self, spreadsheet_id, request, fields=None, batch=False): diff --git a/pygsheets/sheet.py b/pygsheets/sheet.py index 697d3f0..702a265 100644 --- a/pygsheets/sheet.py +++ b/pygsheets/sheet.py @@ -149,8 +149,23 @@ def sheets_copy_to(self, source_spreadsheet_id, worksheet_id, destination_spread **kwargs) return self._execute_requests(request) - def values_append(self): - pass + def values_append(self, spreadsheet_id, values, major_dimension, range, replace): + """wrapper around batch append""" + body = { + 'values': values, + 'majorDimension': major_dimension + } + + if replace: + inoption = "OVERWRITE" + else: + inoption = "INSERT_ROWS" + request = self.service.spreadsheets().values().append(spreadsheetId=spreadsheet_id, range=range, + body=body, + insertDataOption=inoption, + includeValuesInResponse=False, + valueInputOption="USER_ENTERED") + self._execute_requests(request) def values_batch_clear(self): pass diff --git a/pygsheets/worksheet.py b/pygsheets/worksheet.py index 5830cd2..bfbe2db 100755 --- a/pygsheets/worksheet.py +++ b/pygsheets/worksheet.py @@ -796,24 +796,27 @@ def adjust_row_height(self, start, end=None, pixel_size=100): } self.client.sh_batch_update(self.spreadsheet.id, request, batch=self.spreadsheet.batch_mode) - def append_table(self, start='A1', end=None, values=None, dimension='ROWS', overwrite=False): - """Append values to the sheet. + def append_table(self, values, start='A1', end=None, dimension='ROWS', overwrite=False): + """Append a row or column of values. + + #TODO: How does this actually work? + This will append the list of provided values to the Reference: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append + :param values: List of values for the new row or column. :param start: Top left cell of the range (requires a label). :param end: Bottom right cell of the range (requires a label). - :param values: List of values for the new row or column. + :param dimension: Dimension to which the values will be added ('ROWS' or 'COLUMNS') :param overwrite: Overwrite existing values. """ - if type(values[0]) != list: values = [values] if not end: end = (self.rows, self.cols) - body = {"values": values, "majorDimension": dimension} - self.client.sh_append(self.spreadsheet.id, body=body, rranage=self._get_range(start, end), replace=overwrite) + self.client.sheet.values_append(self.spreadsheet.id, values, dimension, range=self._get_range(start, end), + replace=overwrite) def replace(self, pattern, replacement=None, **kwargs): """Replace values in any cells matched by pattern in this worksheet.