Locane
Locane

Reputation: 3134

Python example of how to get formatting information from a cell in Google Sheets API v4?

I've been trying to write my own Google Sheets wrapper, and it's been a frustrating experience so far. The thing I'm stuck on at the moment is how to get a symmetrical in / out format of sheet data.

Basically, I want to call values().get(), alter the resulting hash, and send that same hash back up to update().

I'm happy to write my own solution to process or coerce the output of values().get() to the structure that batchUpdate() needs, but I need the formatting information of each of the cells to do that.

batchUpdate() expects formatting information like this:

bod = {
    'updateCells': {
        'start': {
            'sheetId': 0,
            'rowIndex': 7,
            'columnIndex': 0
        },
        'rows': [
            {
                'values': [
                    {
                        "userEnteredValue": {
                            'stringValue': 'LOL'
                            }, 
                        "userEnteredFormat": {
                            'backgroundColor': {
                                'red': .2,
                                'blue': .75,
                                'green': .75
                            }
                        }
                    },
                    {
                        "userEnteredValue": {
                            'stringValue': 'LOL2'
                            }, 
                        "userEnteredFormat": {
                            'backgroundColor': {
                                'red': .2,
                                'blue': 1,
                                'green': .75
                            }
                        }
                    },
                    {
                        "userEnteredValue": {
                            'stringValue': 'LOL3'
                            }, 
                        "userEnteredFormat": {
                            'backgroundColor': {
                                'red': .2,
                                'blue': 1,
                                'green': 1
                            }
                        }
                    }                    
                ]
            }
        ],
        'fields': 'userEnteredValue,userEnteredFormat.backgroundColor'
    }
}

How I'm retrieving values currently looks something like this:

import requests
import json
from oauth2client.service_account import ServiceAccountCredentials
from apiclient.discovery import build

#Set up credentials object
auth_key_url = "<JSON CREDENTIALS FILE>"
file_contents = requests.get(auth_key_url).content
key_dict = json.loads(file_contents)
creds = ServiceAccountCredentials.from_json_keyfile_dict(key_dict, ['https://spreadsheets.google.com/feeds'])

#Now build the API object
discoveryUrl = "https://sheets.googleapis.com/$discovery/rest?version=v4"
gsheet = build('sheets', 'v4', discoveryServiceUrl=discoveryUrl, credentials=creds)

result = gsheet.spreadsheets().values().get(spreadsheetId="<A SHEET ID>", range="Sheet1!A1:ZZ").execute()    

This produces "results", which is a dictionary with 2 keys, "range" and "values", and "values" is a list of lists of the values of the spreadsheet. These lists do not contain formatting data - just the values in the cells.

Can someone show me, in Python, how I can get cell value, background color, alignment, and other cell formatting information from spreadsheets().values() or from the spreadsheet?

Upvotes: 0

Views: 2517

Answers (1)

Eric Koleda
Eric Koleda

Reputation: 12673

The spreadsheets.values.get endpoint only returns the values. If you want a more complete picture of the spreadsheet (formatting, etc) then you need to use the spreadsheets.get endpoint:

https://developers.google.com/sheets/reference/rest/v4/spreadsheets/get

Make sure to pass either includeGridData=true or pass a value for the fields that includes sheets.data so that the cell data is returned. Pass a value in the range parameter to limit the results to only a specific range.

Upvotes: 3

Related Questions