Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the number of rows in a Google Spreadsheet using api?

Is it possible to fetch the number of filled rows without fetching the whole spreadsheet, or find the first empty row number?

I am using the python api wrapper if it matters.

like image 988
canerbalci Avatar asked May 10 '13 08:05

canerbalci


People also ask

How do I automatically count rows in Google Sheets?

Use autofill to complete a series Highlight the cells. You'll see a small blue box in the lower right corner. Drag the blue box any number of cells down or across. If the cells form a series of dates or numbers, the series will continue across the selected cells.

How do I get the last row in Google Sheets API?

Rather than retrieving all the rows into memory just to get the values in the last row, you can use the append API to append an empty table to the end of the sheet, and then parse the range that comes back in the response. You can then use the index of the last row to request just the data you want.


2 Answers

The main google API docs say you can do it using the WorksheetFeed. So must be possible from python.

Google Data APIs Client Library https://developers.google.com/gdata/javadoc/com/google/gdata/data/spreadsheet/WorksheetFeed (updated link 2021/10/31)

Edit: @alvaro nortes correctly pointed out the link is broken. Update the link.

like image 195
eddyparkinson Avatar answered Sep 19 '22 13:09

eddyparkinson


Using pygsheets, this will return the number of filled rows in the spreadsheet

import pygsheets

gc = pygsheets.authorize(service_file='client_secret.json'). #authorization
worksheet = gc.open('Sign Up').sheet1. #opens the first sheet in "Sign Up"
cells = worksheet.get_all_values(include_tailing_empty_rows=False, include_tailing_empty=False, returnas='matrix')
end_row = len(cells)
print(end_row)
like image 27
Omobolaji Avatar answered Sep 21 '22 13:09

Omobolaji