Skip to content

Commit

Permalink
Merge pull request #1092 from yongrenjie/inherit-from-before
Browse files Browse the repository at this point in the history
inherit_from_before option for insert_row/insert_rows
  • Loading branch information
lavigne958 authored Jul 29, 2022
2 parents 6d52365 + 6d4bee2 commit 912a1b4
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 8 deletions.
78 changes: 72 additions & 6 deletions gspread/worksheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -1337,7 +1337,13 @@ def append_rows(

return self.spreadsheet.values_append(range_label, params, body)

def insert_row(self, values, index=1, value_input_option=ValueInputOption.raw):
def insert_row(
self,
values,
index=1,
value_input_option=ValueInputOption.raw,
inherit_from_before=False,
):
"""Adds a row to the worksheet at the specified index and populates it
with values.
Expand All @@ -1349,12 +1355,33 @@ def insert_row(self, values, index=1, value_input_option=ValueInputOption.raw):
should be interpreted. Possible values are ``ValueInputOption.raw``
or ``ValueInputOption.user_entered``.
See `ValueInputOption`_ in the Sheets API.
:param bool inherit_from_before: (optional) If True, the new row will
inherit its properties from the previous row. Defaults to False,
meaning that the new row acquires the properties of the row
immediately after it.
.. warning::
`inherit_from_before` must be False when adding a row to the top
of a spreadsheet (`index=1`), and must be True when adding to
the bottom of the spreadsheet.
.. _ValueInputOption: https://developers.google.com/sheets/api/reference/rest/v4/ValueInputOption
"""
return self.insert_rows([values], index, value_input_option=value_input_option)
return self.insert_rows(
[values],
index,
value_input_option=value_input_option,
inherit_from_before=inherit_from_before,
)

def insert_rows(self, values, row=1, value_input_option=ValueInputOption.raw):
def insert_rows(
self,
values,
row=1,
value_input_option=ValueInputOption.raw,
inherit_from_before=False,
):
"""Adds multiple rows to the worksheet at the specified index and
populates them with values.
Expand All @@ -1366,6 +1393,16 @@ def insert_rows(self, values, row=1, value_input_option=ValueInputOption.raw):
should be interpreted. Possible values are ``ValueInputOption.raw``
or ``ValueInputOption.user_entered``.
See `ValueInputOption`_ in the Sheets API.
:param bool inherit_from_before: (optional) If true, new rows will
inherit their properties from the previous row. Defaults to False,
meaning that new rows acquire the properties of the row immediately
after them.
.. warning::
`inherit_from_before` must be False when adding rows to the top
of a spreadsheet (`row=1`), and must be True when adding to
the bottom of the spreadsheet.
"""

# can't insert row on sheet with colon ':'
Expand All @@ -1375,6 +1412,11 @@ def insert_rows(self, values, row=1, value_input_option=ValueInputOption.raw):
"can't insert row in worksheet with colon ':' in its name. See issue: https://issuetracker.google.com/issues/36761154"
)

if inherit_from_before and row == 1:
raise GSpreadException(
"inherit_from_before cannot be used when inserting row(s) at the top of a spreadsheet"
)

body = {
"requests": [
{
Expand All @@ -1384,7 +1426,8 @@ def insert_rows(self, values, row=1, value_input_option=ValueInputOption.raw):
"dimension": Dimension.rows,
"startIndex": row - 1,
"endIndex": len(values) + row - 1,
}
},
"inheritFromBefore": inherit_from_before,
}
}
]
Expand All @@ -1400,7 +1443,13 @@ def insert_rows(self, values, row=1, value_input_option=ValueInputOption.raw):

return self.spreadsheet.values_append(range_label, params, body)

def insert_cols(self, values, col=1, value_input_option=ValueInputOption.raw):
def insert_cols(
self,
values,
col=1,
value_input_option=ValueInputOption.raw,
inherit_from_before=False,
):
"""Adds multiple new cols to the worksheet at specified index and
populates them with values.
Expand All @@ -1412,7 +1461,23 @@ def insert_cols(self, values, col=1, value_input_option=ValueInputOption.raw):
should be interpreted. Possible values are ``ValueInputOption.raw``
or ``ValueInputOption.user_entered``.
See `ValueInputOption`_ in the Sheets API.
:param bool inherit_from_before: (optional) If True, new columns will
inherit their properties from the previous column. Defaults to
False, meaning that new columns acquire the properties of the
column immediately after them.
.. warning::
`inherit_from_before` must be False if adding at the left edge
of a spreadsheet (`col=1`), and must be True if adding at the
right edge of the spreadsheet.
"""

if inherit_from_before and col == 1:
raise GSpreadException(
"inherit_from_before cannot be used when inserting column(s) at the left edge of a spreadsheet"
)

body = {
"requests": [
{
Expand All @@ -1422,7 +1487,8 @@ def insert_cols(self, values, col=1, value_input_option=ValueInputOption.raw):
"dimension": Dimension.cols,
"startIndex": col - 1,
"endIndex": len(values) + col - 1,
}
},
"inheritFromBefore": inherit_from_before,
}
}
]
Expand Down
4 changes: 2 additions & 2 deletions tests/cassettes/WorksheetTest.test_insert_row.json
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@
"request": {
"method": "POST",
"uri": "https://sheets.googleapis.com/v4/spreadsheets/1ij8FSXamZqVkz9G8W2HtTsF9DrrMuXxxLX6sHTXipXs:batchUpdate",
"body": "{\"requests\": [{\"insertDimension\": {\"range\": {\"sheetId\": 0, \"dimension\": \"ROWS\", \"startIndex\": 1, \"endIndex\": 2}}}]}",
"body": "{\"requests\": [{\"insertDimension\": {\"range\": {\"sheetId\": 0, \"dimension\": \"ROWS\", \"startIndex\": 1, \"endIndex\": 2}, \"inheritFromBefore\": false}}]}",
"headers": {
"User-Agent": [
"python-requests/2.27.1"
Expand Down Expand Up @@ -746,7 +746,7 @@
"request": {
"method": "POST",
"uri": "https://sheets.googleapis.com/v4/spreadsheets/1ij8FSXamZqVkz9G8W2HtTsF9DrrMuXxxLX6sHTXipXs:batchUpdate",
"body": "{\"requests\": [{\"insertDimension\": {\"range\": {\"sheetId\": 0, \"dimension\": \"ROWS\", \"startIndex\": 0, \"endIndex\": 1}}}]}",
"body": "{\"requests\": [{\"insertDimension\": {\"range\": {\"sheetId\": 0, \"dimension\": \"ROWS\", \"startIndex\": 0, \"endIndex\": 1}, \"inheritFromBefore\": false}}]}",
"headers": {
"User-Agent": [
"python-requests/2.27.1"
Expand Down
4 changes: 4 additions & 0 deletions tests/worksheet_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,10 @@ def test_insert_row(self):
b3 = self.sheet.acell("B3", value_render_option=utils.ValueRenderOption.formula)
self.assertEqual(b3.value, formula)

new_row_values = [next(sg) for i in range(num_cols + 4)]
with pytest.raises(GSpreadException):
self.sheet.insert_row(new_row_values, 1, inherit_from_before=True)

@pytest.mark.vcr()
def test_delete_row(self):
sg = self._sequence_generator()
Expand Down

0 comments on commit 912a1b4

Please sign in to comment.