Google API Explorer for Spreadsheet返回属性-如何在Python Quickstart中获取属性(文件名)?
当我使用toGoogle API Explorer for Spreadsheet返回属性-如何在Python Quickstart中获取属性(文件名)?,python,google-sheets,google-spreadsheet-api,google-api-explorer,Python,Google Sheets,Google Spreadsheet Api,Google Api Explorer,当我使用to尝试此API并填写电子表格ID时,它返回的信息包括一个包含电子表格名称的properties键: { "spreadsheetId": "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms", "properties": { "title": "Example Spreadsheet", "locale": "en_US", } } 如何使用Python获取此信息? 我想从以下位置获取文件名:result['prop
尝试此API
并填写电子表格ID时,它返回的信息包括一个包含电子表格名称的properties
键:
{
"spreadsheetId": "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms",
"properties": {
"title": "Example Spreadsheet",
"locale": "en_US",
}
}
如何使用Python获取此信息?
我想从以下位置获取文件名:result['properties']['title']
代码是:
结果
这里是请求的范围名称中的数据散列:
{
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'
}
它不包含我想要的属性
字典!我打开了Google Sheets API,并请求了以下范围,其中也不包括属性
信息:
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',
]
您想从电子表格ID检索文件名。如果我的理解正确,这个答案如何?我认为有两种模式可以实现你想要做的事情。这些都是使用表API和驱动API 1.使用工作表API
- 请从
修改为spreadsheets().values().get()
spreadsheets().get()
- 在您的情况下,
可用于检索标题(文件名)fields=“properties/title”
- 作为另一种方法,您可以使用驱动器API从文件ID检索文件名
- 当您使用上述脚本时,请再次确认是否在API控制台启用了工作表API和驱动器API
- 如果发生与作用域相关的错误,请删除一次
,然后通过再次授权重新创建文件。然后再次运行脚本credentials.json
如果我误解了您的问题,很抱歉。您想从电子表格ID检索文件名。如果我的理解正确,这个答案如何?我认为有两种模式可以实现你想要做的事情。这些都是使用表API和驱动API 1.使用工作表API
- 请从
修改为spreadsheets().values().get()
spreadsheets().get()
- 在您的情况下,
可用于检索标题(文件名)fields=“properties/title”
- 作为另一种方法,您可以使用驱动器API从文件ID检索文件名
- 当您使用上述脚本时,请再次确认是否在API控制台启用了工作表API和驱动器API
- 如果发生与作用域相关的错误,请删除一次
,然后通过再次授权重新创建文件。然后再次运行脚本credentials.json
如果我误解了你的问题,很抱歉。你的示例代码调用了
电子表格.values
方法。这将返回值,而不是元数据。您的示例链接调用了电子表格.get
方法。为什么你认为它们应该返回相同的数据?@tehhowch,因为我没有看到在两次调用之间添加了值这个词/耸耸肩说太晚了。@tehhowch另外,我希望我可以通过一个调用同时获得两条信息,而不是调用API来获取文件属性,调用API来获取值范围的数据,因为Google API Explorer for Spreadsheet似乎可以做到这一点,但我无法从该页面中找出正确的Python调用是什么。如果您请求一个范围,并在spreadsheets().get
调用中,您可以获得值信息(请参见上的示例Python代码)示例代码调用spreadsheet.values
方法。这将返回值,而不是元数据。您的示例链接调用了电子表格.get
方法。为什么你认为它们应该返回相同的数据?@tehhowch,因为我没有看到在两次调用之间添加了值这个词/耸耸肩说太晚了。@tehhowch另外,我希望我可以通过一个调用同时获得两条信息,而不是调用API来获取文件属性,调用API来获取值范围的数据,因为Google API Explorer for Spreadsheet似乎可以做到这一点,但我无法从该页面中找出正确的Python调用是什么。如果您请求一个范围,并在电子表格()中获取值信息(请参阅上的示例Python代码)
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',
]
"""
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)
{'properties': {'title': '### filename ###'}}
"""
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)
{'name': '### filename ###'}