Search code examples
pythongoogle-apps-scriptgoogle-sheetsgspread

Python GSpread - Loop through rows


I am trying to loop through the rows on a Google sheet using python and gspread. Every time I try to get the values, the list is returned as columns and not rows.

Is there a way to retrieve the rows and loop through them?

Looking here https://docs.gspread.org/en/latest/user-guide.html#getting-all-values-from-a-row-or-a-column I am not too sure how many rows there will be. Is there a way to count the first column?


Solution

  • Import, Authenticate and Open the Sheet as defined by the gspread documentation:

    import gspread
    gc = gspread.service_account()
    worksheet = gc.open("Sheet1").sheet1
    

    Then use .get_all_values() to iterate through the rows:

    rows = worksheet.get_all_values()
    for row in rows:
       # ...