From 52eef450910046a4fdac641aed456dbe986defe3 Mon Sep 17 00:00:00 2001 From: Alexey Dmitriev Date: Tue, 20 Oct 2020 11:56:34 +0300 Subject: [PATCH] Add insert_cols method to worksheet (#802) Closes #798 --- gspread/models.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/gspread/models.py b/gspread/models.py index c795487ef..baf762864 100644 --- a/gspread/models.py +++ b/gspread/models.py @@ -1559,6 +1559,43 @@ def insert_rows(self, values, row=1, value_input_option='RAW'): return self.spreadsheet.values_append(range_label, params, body) + def insert_cols(self, values, col=1, value_input_option='RAW'): + """Adds multiple new cols to the worksheet at specified index and + populates them with values. + + :param list values: List of col lists. a list of lists, with the lists + each containing one col's values. Increases the number of rows + if there are more values than columns. + :param int row: Start col to update(one-based). Defaults to 1 (one). + :param str value_input_option: (optional) Determines how input data + should be interpreted. Possible values are ``RAW`` or + ``USER_ENTERED``. See `ValueInputOption`_ in the Sheets API. + """ + body = { + "requests": [ + { + "insertDimension": { + "range": { + "sheetId": self.id, + "dimension": "COLUMNS", + "startIndex": col - 1, + "endIndex": len(values) + col - 1, + } + } + } + ] + } + + self.spreadsheet.batch_update(body) + + range_label = absolute_range_name(self.title, rowcol_to_a1(1, col)) + + params = {'valueInputOption': value_input_option} + + body = {'majorDimension': 'COLUMNS', '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.