From b09732d8fa523a52c659f677c0e9767affb1d71a Mon Sep 17 00:00:00 2001 From: tr-fi <55879803+tr-fi@users.noreply.github.com> Date: Sat, 21 Mar 2020 12:35:25 +0700 Subject: [PATCH 1/6] Adds multiple rows with index Adds multiple rows to the worksheet at the specified index and populates it with values --- gspread/models.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/gspread/models.py b/gspread/models.py index 5afb73e20..eee1911c3 100644 --- a/gspread/models.py +++ b/gspread/models.py @@ -1287,6 +1287,43 @@ def append_row( return self.spreadsheet.values_append(range_label, params, body) + def append_rows(self, values, index=1, value_input_option='RAW'): + """Adds multiple rows to the worksheet at the specified index + and populates it with values. + The input should be a list of lists, with the lists each + containing one row's values. + Widens the worksheet if there are more values than columns. + :param values: List of row lists. + """ + + body = { + "requests": [{ + "insertDimension": { + "range": { + "sheetId": self.id, + "dimension": "ROWS", + "startIndex": index - 1, + "endIndex": index + } + } + }] + } + + self.spreadsheet.batch_update(body) + + range_label = '%s!%s' % (self.title, 'A%s' % index) + + params = { + 'valueInputOption': value_input_option + } + + body = { + 'majorDimension': 'ROWS', + 'values': values + } + + return self.spreadsheet.values_append(range_label, params, body) + def insert_row( self, values, From 4837075392e3271e5e2ea10741b16e932fc1b3a5 Mon Sep 17 00:00:00 2001 From: tr-fi <55879803+tr-fi@users.noreply.github.com> Date: Sat, 21 Mar 2020 16:54:13 +0700 Subject: [PATCH 2/6] Update models.py --- gspread/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gspread/models.py b/gspread/models.py index eee1911c3..b570eacd1 100644 --- a/gspread/models.py +++ b/gspread/models.py @@ -1303,7 +1303,7 @@ def append_rows(self, values, index=1, value_input_option='RAW'): "sheetId": self.id, "dimension": "ROWS", "startIndex": index - 1, - "endIndex": index + "endIndex": len(values) } } }] From 1e36cf6728d2a979fbd14a6d58c547dc6ff9c2bf Mon Sep 17 00:00:00 2001 From: tr-fi <55879803+tr-fi@users.noreply.github.com> Date: Sat, 21 Mar 2020 23:49:05 +0700 Subject: [PATCH 3/6] Update models.py --- gspread/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gspread/models.py b/gspread/models.py index b570eacd1..eece51c35 100644 --- a/gspread/models.py +++ b/gspread/models.py @@ -1303,7 +1303,7 @@ def append_rows(self, values, index=1, value_input_option='RAW'): "sheetId": self.id, "dimension": "ROWS", "startIndex": index - 1, - "endIndex": len(values) + "endIndex": len(values) + 1 } } }] From 8adedced19e6c3b3aaddc80d398c7b418d56df0f Mon Sep 17 00:00:00 2001 From: tr-fi <55879803+tr-fi@users.noreply.github.com> Date: Tue, 21 Apr 2020 18:02:51 +0700 Subject: [PATCH 4/6] Update models.py change method name --- gspread/models.py | 74 +++++++++++++++++++++++------------------------ 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/gspread/models.py b/gspread/models.py index eece51c35..6efb8b333 100644 --- a/gspread/models.py +++ b/gspread/models.py @@ -1287,43 +1287,6 @@ def append_row( return self.spreadsheet.values_append(range_label, params, body) - def append_rows(self, values, index=1, value_input_option='RAW'): - """Adds multiple rows to the worksheet at the specified index - and populates it with values. - The input should be a list of lists, with the lists each - containing one row's values. - Widens the worksheet if there are more values than columns. - :param values: List of row lists. - """ - - body = { - "requests": [{ - "insertDimension": { - "range": { - "sheetId": self.id, - "dimension": "ROWS", - "startIndex": index - 1, - "endIndex": len(values) + 1 - } - } - }] - } - - self.spreadsheet.batch_update(body) - - range_label = '%s!%s' % (self.title, 'A%s' % index) - - params = { - 'valueInputOption': value_input_option - } - - body = { - 'majorDimension': 'ROWS', - 'values': values - } - - return self.spreadsheet.values_append(range_label, params, body) - def insert_row( self, values, @@ -1376,6 +1339,43 @@ def insert_row( return data + def insert_rows(self, values, index=1, value_input_option='RAW'): + """Adds multiple rows to the worksheet at the specified index + and populates it with values. + The input should be a list of lists, with the lists each + containing one row's values. + Widens the worksheet if there are more values than columns. + :param values: List of row lists. + """ + + body = { + "requests": [{ + "insertDimension": { + "range": { + "sheetId": self.id, + "dimension": "ROWS", + "startIndex": index - 1, + "endIndex": len(values) + 1 + } + } + }] + } + + self.spreadsheet.batch_update(body) + + range_label = '%s!%s' % (self.title, 'A%s' % index) + + params = { + 'valueInputOption': value_input_option + } + + body = { + 'majorDimension': 'ROWS', + 'values': values + } + + return self.spreadsheet.values_append(range_label, params, body) + def delete_row(self, index): """"Deletes the row from the worksheet at the specified index. From fa7488f69889fe69d8d2b2f31fe41865dd344218 Mon Sep 17 00:00:00 2001 From: tr-fi <55879803+tr-fi@users.noreply.github.com> Date: Tue, 21 Apr 2020 20:36:19 +0700 Subject: [PATCH 5/6] Update models.py fix indentation line wrong --- gspread/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gspread/models.py b/gspread/models.py index 6efb8b333..9971e43dc 100644 --- a/gspread/models.py +++ b/gspread/models.py @@ -1339,7 +1339,7 @@ def insert_row( return data - def insert_rows(self, values, index=1, value_input_option='RAW'): + def insert_rows(self, values, index=1, value_input_option='RAW'): """Adds multiple rows to the worksheet at the specified index and populates it with values. The input should be a list of lists, with the lists each From d8b33a67ef14de98eeb5dad48a55f02043d7f801 Mon Sep 17 00:00:00 2001 From: tr-fi <55879803+tr-fi@users.noreply.github.com> Date: Mon, 27 Apr 2020 20:32:55 +0700 Subject: [PATCH 6/6] Update models.py Fix index not working exactly --- gspread/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gspread/models.py b/gspread/models.py index 9971e43dc..7a99c28ca 100644 --- a/gspread/models.py +++ b/gspread/models.py @@ -1355,7 +1355,7 @@ def insert_rows(self, values, index=1, value_input_option='RAW'): "sheetId": self.id, "dimension": "ROWS", "startIndex": index - 1, - "endIndex": len(values) + 1 + "endIndex": len(values) + index - 1 } } }]