Search code examples
pythongoogle-sheetsgoogle-sheets-apigoogle-api-explorer

Google API Explorer for Spreadsheet returns properties - How do I get properties (filename) in Python Quickstart?


When I use the Google API Explorer for SpreadSheet to Try this API and fill in the spreadsheetId, the information it returns includes a properties key that has the spreadsheet name in it:

{
  "spreadsheetId": "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms",
  "properties": {
    "title": "Example Spreadsheet",
    "locale": "en_US",
  }
}

How do I get this information using Python?

I want to get the filename from : result['properties']['title']

The Python quickstart code is:

"""
Shows basic usage of the Sheets API. Prints values from a Google Spreadsheet.
"""
from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools

# Setup the Sheets API
SCOPES = 'https://www.googleapis.com/auth/spreadsheets.readonly'
store = file.Storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store)
service = build('sheets', 'v4', http=creds.authorize(Http()))

# Call the Sheets API
SPREADSHEET_ID = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms'
RANGE_NAME = 'Class Data!A2:E'
result = service.spreadsheets().values().get(spreadsheetId=SPREADSHEET_ID,
                                             range=RANGE_NAME).execute()

The result here is a hash of the data from the RANGE_NAME that was requested:

{
   u'range': u "'Class Data'!A2:E101", 
   u'values': [
    [u 'Alexandra', u 'Female', u '4. Senior', u 'CA', u 'English'],
    [u 'Andrew', u 'Male', u '1. Freshman', u 'SD', u 'Math'],
    [u 'Anna', u 'Female', u '1. Freshman', u 'NC', u 'English'],
    [u 'Becky', u 'Female', u '2. Sophomore', u 'SD', u 'Art'],
    [u 'Benjamin', u 'Male', u '4. Senior', u 'WI', u 'English'],
    [u 'Carl', u 'Male', u '3. Junior', u 'MD', u 'Art'],
    [u 'Carrie', u 'Female', u '3. Junior', u 'NE', u 'English'],
    [u 'Dorothy', u 'Female', u '4. Senior', u 'MD', u 'Math'],
    [u 'Dylan', u 'Male', u '1. Freshman', u 'MA', u 'Math'],
    [u 'Edward', u 'Male', u '3. Junior', u 'FL', u 'English'],
    [u 'Ellen', u 'Female', u '1. Freshman', u 'WI', u 'Physics'],
    [u 'Fiona', u 'Female', u '1. Freshman', u 'MA', u 'Art'],
    [u 'John', u 'Male', u '3. Junior', u 'CA', u 'Physics'],
    [u 'Jonathan', u 'Male', u '2. Sophomore', u 'SC', u 'Math'],
    [u 'Joseph', u 'Male', u '1. Freshman', u 'AK', u 'English'],
    [u 'Josephine', u 'Female', u '1. Freshman', u 'NY', u 'Math'],
    [u 'Karen', u 'Female', u '2. Sophomore', u 'NH', u 'English'],
    [u 'Kevin', u 'Male', u '2. Sophomore', u 'NE', u 'Physics'],
    [u 'Lisa', u 'Female', u '3. Junior', u 'SC', u 'Art'],
    [u 'Mary', u 'Female', u '2. Sophomore', u 'AK', u 'Physics'],
    [u 'Maureen', u 'Female', u '1. Freshman', u 'CA', u 'Physics'],
    [u 'Nick', u 'Male', u '4. Senior', u 'NY', u 'Art'],
    [u 'Olivia', u 'Female', u '4. Senior', u 'NC', u 'Physics'],
    [u 'Pamela', u 'Female', u '3. Junior', u 'RI', u 'Math'],
    [u 'Patrick', u 'Male', u '1. Freshman', u 'NY', u 'Art'],
    [u 'Robert', u 'Male', u '1. Freshman', u 'CA', u 'English'],
    [u 'Sean', u 'Male', u '1. Freshman', u 'NH', u 'Physics'],
    [u 'Stacy', u 'Female', u '1. Freshman', u 'NY', u 'Math'],
    [u 'Thomas', u 'Male', u '2. Sophomore', u 'RI', u 'Art'],
    [u 'Will', u 'Male', u '4. Senior', u 'FL', u 'Math']
  ], 
  u 'majorDimension': u 'ROWS'
}

It does not contain the properties dictionary that I want! I turned on the Google Sheets API and I've requested the following SCOPES and it also does not include the properties information:

SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly',
      'https://www.googleapis.com/auth/drive.readonly',
      'https://www.googleapis.com/auth/drive',
      'https://www.googleapis.com/auth/drive.file',
      'https://www.googleapis.com/auth/spreadsheets',
      ]

Solution

  • You want to retrieve the filename from Spreadsheet ID. If my understanding is correct, how about this answer? I think that there are 2 patterns for achieving what you want do. Those are the use of Sheets API and Drive API.

    1. Using Sheets API

    • Please modify from spreadsheets().values().get() to spreadsheets().get().
    • In your case, fields="properties/title" can be used for retrieving the title (filename).

    Modified script :

    """
    Shows basic usage of the Sheets API. Prints values from a Google Spreadsheet.
    """
    from __future__ import print_function
    from apiclient.discovery import build
    from httplib2 import Http
    from oauth2client import file, client, tools
    
    # Setup the Sheets API
    SCOPES = 'https://www.googleapis.com/auth/spreadsheets.readonly'
    store = file.Storage('credentials.json')
    creds = store.get()
    if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
        creds = tools.run_flow(flow, store)
    service = build('sheets', 'v4', http=creds.authorize(Http()))
    
    # Call the Sheets API
    SPREADSHEET_ID = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms'
    result = service.spreadsheets().get(spreadsheetId=SPREADSHEET_ID,
                                        fields="properties/title").execute()
    print(result)
    

    Result :

    {'properties': {'title': '### filename ###'}}
    

    2. Using Drive API

    • As another method, you can retrieve the filename from file ID using Drive API.

    Script :

    """
    Shows basic usage of the Sheets API. Prints values from a Google Spreadsheet.
    """
    from __future__ import print_function
    from apiclient.discovery import build
    from httplib2 import Http
    from oauth2client import file, client, tools
    
    # Setup the Sheets API
    SCOPES = 'https://www.googleapis.com/auth/drive.readonly'
    store = file.Storage('credentials.json')
    creds = store.get()
    if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
        creds = tools.run_flow(flow, store)
    service = build('drive', 'v3', http=creds.authorize(Http()))
    
    # Call the Sheets API
    SPREADSHEET_ID = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms'
    result = service.files().get(fileId=SPREADSHEET_ID,
                                 fields="name").execute()
    print(result)
    

    Result :

    {'name': '### filename ###'}
    

    Note :

    • When you use above scripts, please confirm whether Sheets API and Drive API are enabled at API console, again.
    • If the error related to the scopes occurs, please remove credentials.json once and recreate the file by authorizing again. And run the scripts again.

    References :

    If I misunderstand your question, I'm sorry.