Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds multiple rows with index #734

Merged
merged 6 commits into from
Apr 27, 2020
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions gspread/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1339,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,
Copy link
Owner

Choose a reason for hiding this comment

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

It looks like there's a problem with insert indices.

Steps to reproduce:

  1. I have a sheet with consecutive values:

Screenshot 2020-04-27 at 15 00 55

  1. I call
wks.insert_rows([['r1.1','r1.2'],['r2.1', 'r2.2']], index=3)

Where wks is my worksheet.

  1. I get

Screenshot 2020-04-27 at 15 01 20

Where the expected result is 1, 2, r1.1, r2.1, 3, 4, etc in the column A.
It looks like the method overwrites the cell with value "3".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry I did not test thoroughly. I fixed it but need you test again

Copy link
Owner

Choose a reason for hiding this comment

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

No problem, thank you for fast response

"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.

Expand Down