Skip to content

Commit

Permalink
Add insert_cols method to worksheet (#802)
Browse files Browse the repository at this point in the history
Closes #798
  • Loading branch information
AlexeyDmitriev committed Oct 20, 2020
1 parent db2a424 commit 52eef45
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions gspread/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

2 comments on commit 52eef45

@abubelinha
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess line 1569 should say :param int col: instead of :param int row: ?

@burnash
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@abubelinha Thank you for spotting this, fixed in ced63fe

Please sign in to comment.