Skip to content

Instantly share code, notes, and snippets.

@glenwatson
Last active August 29, 2015 14:07
Show Gist options
  • Save glenwatson/6fcddc1898baca8b9de1 to your computer and use it in GitHub Desktop.
Save glenwatson/6fcddc1898baca8b9de1 to your computer and use it in GitHub Desktop.
def insert_row_at(self, values, row_idx):
""""Adds a row to the beginning of the worksheet and populates it with values.
Widens the worksheet if there are more values than columns.
:param values: List of values for the new row.
"""
self.add_rows(1)
data_width = len(values)
if self.col_count < data_width:
self.resize(cols=data_width)
all_cells = self.get_all_values()
lower_rows = [row:len(all_cells)]
lower_rows.insert(0, values)
updated_cell_list=[]
for r, row in enumerate(lower_rows, start=row):
for c, cell in enumerate(row, start=1):
ncell = self.cell(r, c)
ncell.value = all_cells[r-1][c-1]
updated_cell_list.append(ncell)
self.update_cells(updated_cell_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment