Skip to content

Instantly share code, notes, and snippets.

@maksam07
Last active June 20, 2023 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maksam07/b77f00f6b7d3e4955266a386dafa515d to your computer and use it in GitHub Desktop.
Save maksam07/b77f00f6b7d3e4955266a386dafa515d to your computer and use it in GitHub Desktop.
pygsheets 2.0.6. How to find the first empty row of a google spread sheet using python GSPREAD? cols - this means what range of columns must be empty in the whole row for it to be considered writable. See https://stackoverflow.com/a/76502396/2166371
def next_available_row(sheet, cols=None):
cols = sorted([1, 2] if not isinstance(cols, list) or len(cols) != 2 else cols)
cols = sheet.get_values(
(1, cols[0]), (sheet.rows, cols[1]),
returnas='cells', include_tailing_empty_rows=True
)
return max([cell.address.index[0] for row in cols for cell in row if cell.value.strip()]) + 1
gc = pygsheets.authorize()
sh = gc.open_by_key('***')
ws = sh.worksheet_by_title('***')
next_row = next_available_row(ws, [1, 3])
print(next_row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment