gpt4 book ai didi

Python quickstart.py 适用于我的 Google 表格电子表格

转载 作者:太空宇宙 更新时间:2023-11-04 04:58:01 25 4
gpt4 key购买 nike

我使用 Python27 从这里: https://developers.google.com/sheets/api/quickstart/python?refresh=1

然后运行示例代码,quickstart.py,ok。

我安装了 install --upgrade google-api-python-client。

我天真地以为只要更改电子表格 ID 和目标范围就可以了。错了!

这是我的代码:

from __future__ import print_function
import httplib2
import os

from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage

try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None

# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/sheets.googleapis.com-python-quickstart.json
SCOPES = 'https://www.googleapis.com/auth/spreadsheets.readonly'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Google Sheets API Python Quickstart'


def get_credentials():
"""Gets valid user credentials from storage.

If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.

Returns:
Credentials, the obtained credential.
"""
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'sheets.googleapis.com-python-quickstart.json')

store = Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials

def main():
"""Shows basic usage of the Sheets API.

Creates a Sheets API service object and prints the names and majors of
students in a sample spreadsheet:

https://docs.google.com/spreadsheets/d/ \ #continued next line
1ksrW3mUJvPJkmv1HZyWC9Ma1Fbe1cX0CDegSjW2yIAY/edit
"""
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
discoveryUrl = ('https://sheets.googleapis.com/$discovery/rest?'
'version=v4')
service = discovery.build('sheets', 'v4', http=http,
discoveryServiceUrl=discoveryUrl)

spreadsheetId = '1ksrW3mUJvPJkmv1HZyWC9Ma1Fbe1cX0CDegSjW2yIAY/edit#gid=0'
rangeName = 'Class Data!B4:C9'
result = service.spreadsheets().values().get(
spreadsheetId=spreadsheetId, range=rangeName).execute()
values = result.get('values', [])

if not values:
print('No data found.')
else:
print('Name, Major:')
for row in values:
# Print columns B and C, which correspond to indices 2 and 3.
print('%s, %s' % (row[1], row[2]))


if __name__ == '__main__':
main()

感谢任何帮助!

最佳答案

您的脚本有效。但是必须修改一个变量。

发件人:

spreadsheetId = '1ksrW3mUJvPJkmv1HZyWC9Ma1Fbe1cX0CDegSjW2yIAY/edit#gid=0'

收件人:

spreadsheetId = '1ksrW3mUJvPJkmv1HZyWC9Ma1Fbe1cX0CDegSjW2yIAY'

注意事项:

您的电子表格 ID 是 1ksrW3mUJvPJkmv1HZyWC9Ma1Fbe1cX0CDegSjW2yIAY。电子表格ID的详细信息是here .

如果您在进行上述修改后脚本无法运行,请随时告诉我。届时,请向我们展示错误信息。

编辑:

“B4:C9”中的数据是一个 2 行 6 列的列表。 list第一个索引为0 错误原因 main() Line 78 in main print('%s, %s' % (row[1], row[2])) IndexError: List index out范围是这个。所以请修改如下。

发件人:

print('%s, %s' % (row[1], row[2]))

收件人:

print('%s, %s' % (row[0], row[1]))

关于Python quickstart.py 适用于我的 Google 表格电子表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46576389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com