如何通过 Python 中的 Google Sheets API v4 在 spreadsheet 中调用特定的 sheet
How to call a specific sheet within a spreadsheet via the Google Sheets API v4 in Python
我对这些东西很陌生,而且我在我的早期项目中确实碰壁了。
我已尽最大努力到处寻找,包括 - 搜索引擎、Whosebug、API 文档,但我真的无法在任何地方找到答案。所以希望问的合适
我正在尝试查看 Google sheet 的特定 sheet(底部的选项卡)的范围。我知道这个 sheet 有一个可以在 url 中找到的 sheetId。我可以得到这个,但是,我绝对找不到用这个号码请求特定 sheet 的方法。我找到了使用此号码复制和复制 sheet 的方法,我什至找到了一种打印所有可用 sheetId 的方法sheet。
def suggestion():
SAMPLE_SPREADSHEET_ID = 'mySpreadsheetID'
SAMPLE_RANGE_NAME = 'A1:D12'
"""Shows basic usage of the Sheets API.
Prints values from a sample spreadsheet.
"""
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('creds.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('sheets', 'v4', http=creds.authorize(Http()))
# Call the Sheets API
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
range=SAMPLE_RANGE_NAME,).execute()
values = result.get('values', [])
if not values:
print('No data found.')
else:
for row in values:
# Print columns A and E, which correspond to indices 0 and 4.
print(row)
我相信答案可能就在这里https://developers.google.com/resources/api-libraries/documentation/sheets/v4/python/latest/sheets_v4.spreadsheets.values.html,但我似乎找不到。
现在程序正在打开默认 sheet(底部的选项卡),而我希望它打开特定 sheet 之一并在那里查找数据。
提前感谢您的帮助。大加赞赏!
Google sheets 使用 A1 notation 指定范围。这可以包括您尝试从中访问值的特定选项卡。
您可以通过在范围前添加 sheet 的名称来指定范围值中的制表符。例如,如果您想要 Sheet2 中的内容:
SAMPLE_RANGE = 'Sheet2!A2:E10'
如果重命名 sheet 选项卡,则需要更新范围以匹配它,因为它是按名称引用的。
我对这些东西很陌生,而且我在我的早期项目中确实碰壁了。
我已尽最大努力到处寻找,包括 - 搜索引擎、Whosebug、API 文档,但我真的无法在任何地方找到答案。所以希望问的合适
我正在尝试查看 Google sheet 的特定 sheet(底部的选项卡)的范围。我知道这个 sheet 有一个可以在 url 中找到的 sheetId。我可以得到这个,但是,我绝对找不到用这个号码请求特定 sheet 的方法。我找到了使用此号码复制和复制 sheet 的方法,我什至找到了一种打印所有可用 sheetId 的方法sheet。
def suggestion():
SAMPLE_SPREADSHEET_ID = 'mySpreadsheetID'
SAMPLE_RANGE_NAME = 'A1:D12'
"""Shows basic usage of the Sheets API.
Prints values from a sample spreadsheet.
"""
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('creds.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('sheets', 'v4', http=creds.authorize(Http()))
# Call the Sheets API
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
range=SAMPLE_RANGE_NAME,).execute()
values = result.get('values', [])
if not values:
print('No data found.')
else:
for row in values:
# Print columns A and E, which correspond to indices 0 and 4.
print(row)
我相信答案可能就在这里https://developers.google.com/resources/api-libraries/documentation/sheets/v4/python/latest/sheets_v4.spreadsheets.values.html,但我似乎找不到。
现在程序正在打开默认 sheet(底部的选项卡),而我希望它打开特定 sheet 之一并在那里查找数据。
提前感谢您的帮助。大加赞赏!
Google sheets 使用 A1 notation 指定范围。这可以包括您尝试从中访问值的特定选项卡。
您可以通过在范围前添加 sheet 的名称来指定范围值中的制表符。例如,如果您想要 Sheet2 中的内容:
SAMPLE_RANGE = 'Sheet2!A2:E10'
如果重命名 sheet 选项卡,则需要更新范围以匹配它,因为它是按名称引用的。