Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google spreadsheets gspread append_row issue

Tags:

python

gspread

I'm working on a program that generates a dynamic google spreadsheet report.

Sometimes when I create a new row (with data) in google spreadsheet using gspread append_row function it doesn't work as expected and no exception is thrown. The new line is added but there is no data inside.

example code below:

#!/usr/bin/python
import gspread

# report line data
report_line = ['name', 'finished <None or int>', 'duration <str>', 'id']

connection = gspread.login('[email protected]', 'password')
spreadsheet = connection.open('report_name')
sheet = spreadsheet.sheet1
sheet.append_row(report_line)

Am I missing something? Is this a known issue? How can I be certain that the append_row function completes successfully?

like image 863
Lior Mizrahi Avatar asked Nov 25 '14 11:11

Lior Mizrahi


2 Answers

It appends a new row after the last row in the sheet. A sheet has 1000 rows by default so you should find your appended row at index=1001.

Try to resize the sheet to the number of rows that are present:

sheet.resize(1)

You should now be able to append rows at the end of your data rather than at the end of the sheet. The number of rows has to be >= 1.

like image 160
BFTM Avatar answered Sep 30 '22 19:09

BFTM


I'm adding to @BFTM's answer:

  1. make sure you are doing the sheet.resize(1) one time, and not everytime you want to add append a row (it will delete all the rows you wrote beyond 1)
  2. To get the number of rows dynamically, in case you don't know to do it manually, you can look at this answer. (get values from one column and find the length)
  3. If you have direct accsess to the spreadsheet you can just delete the empty rows once, and then use append_row() as usual.
like image 29
Alon Gouldman Avatar answered Sep 30 '22 21:09

Alon Gouldman