Search code examples
pythongoogle-sheetsgoogle-sheets-apichunks

Read google sheet by chunk


I have a large size google sheet. I read it from Python API. When load all data into memory, it run out of usage.

spreadsheet = sheets_service.spreadsheets().get(spreadsheetId=sheet_id, includeGridData=True).execute()

It there any way that I can get this sheet by a given chunk size?


Solution

  • The ranges request parameter allows you to retrieve the specific info you want from your sheet. See this code as an example:

    ss_id = 'your-sheet-id'
    # The range must be in A1 notation
    ranges = "A1:B5"   
    grid_data = True
    
    sheet_range = service.spreadsheets()\
            .get(spreadsheetId=ss_id, ranges=ranges, includeGridData=grid_data).execute()
        
    print(sheet_range)
    

    Docs

    You can see more about the Sheets API in these links and you can even test it using the Try this API: