1

In gspread, is it possible to get the last row or cell number that is filled?

The API reference page doesn't seem to help.

H. Kamran
  • 366
  • 2
  • 12

2 Answers2

1

Gspread has "row_count", but I am not sure it returns all rows in a spreadsheet, or just the filled ones. If that doesn't help, there is a slightly less direct, but completely functional way to do it:

1.) let's assume your data is in column A, and it has been filled in consecutively row by row (i.e. no skipped rows)

2.) in another free cell in your spreadsheet--let's assume cell B1--use the native Google Sheets function COUNTA, which will count the number of values in a dataset, and specify column A as the target, i.e. "=COUNTA(A:A)"

3.) now just request the value of cell B1 with gspread's acell, i.e. "last_row_updated = myWorksheet.acell("B1").value"

verbage
  • 51
  • 2
0

You can use the following code, taken from this answer to this similar question:

def last_filled_row(worksheet):
    str_list = list(filter(None, worksheet.col_values(1)))
    return len(str_list)
Carlos Pinzón
  • 1,286
  • 2
  • 15
  • 24