python-3.xgoogle-sheetsgoogle-api-client

How to retrieve all information of google sheets within a range? (The text and style)


I know you can get information in a certain range in a Google Sheets like so

        data = sheets.values().get(spreadsheetId=SHEET_ID, range=SHEET_RANGE).execute()

But when I print data, it doesn't include all the information within the sheets. More specifically, I also want to know about the text format, like the size of the text for example, as well as the size of the columns and rows, the background of the cells, etc.

How do I get all of this information?


Solution

  • Suggestion: Use spreadsheets.get()

    The spreadsheets.values().get() method only returns the values of the indicated range. To retrieve other data, like the cells' formatting, I recommend using spreadsheets.get() instead.

    Try calling the Spreadsheets API using the following:

    #The parameter includeGridData returns the properties and values of the cells
    #within SHEETS_RANGE.
    
    result = sheets.get(spreadsheetId=SHEET_ID, ranges=SHEET_RANGE,
                        includeGridData=True).execute()
    
    #In order to retrieve the cells' values and formatting, 
    #we need to first get the sheet via its index, sheets[0] in this example, 
    #then get the 'data' field
    data = result.get('sheets')[0].get('data')
    

    Reference: