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.
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.
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.
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)
I think This will work. You need to use ListFeed getEntries() to find rows having entry.
URL listFeedUrl = worksheets.get(x).getListFeedUrl();
ListFeed feed = googleservice.getFeed(listFeedUrl, ListFeed.class);
System.out.println("Number of filled rows"+feed_L.getEntries().size()+1);
System.out.println("First Empty row"+feed_L.getEntries().size()+2);
I Assume there is no blank row in between the rows. Blank cells cause no issues, I mean a row must have at least one cell filled.
You can use the library Sheetfu and its method get_data_range
. It basically returns the range that contains data only.
from sheetfu import SpreadsheetApp
sa = SpreadsheetApp('path/to/secret.json')
spreadsheet = sa.open_by_id(spreadsheet_id='<spreadsheet id>')
sheet = spreadsheet.get_sheet_by_name('Sheet1')
data_range = sheet.get_data_range()
This will calculate the coordinates the range that contains data. You can then set the values, notes, backgrounds, etc.. using the following:
values = data_range.get_values()
notes = data_range.get_notes()
backgrounds = data_range.get_backgrounds()
The Range object contains plenty of methods. Check the documentation here.