Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Google工作表上测试一个单元格并写入该行_Python_Python 3.x_Drive - Fatal编程技术网

Python 在Google工作表上测试一个单元格并写入该行

Python 在Google工作表上测试一个单元格并写入该行,python,python-3.x,drive,Python,Python 3.x,Drive,我有一张谷歌表格,上面有学生的名字(列a和B),旁边的列中有一些值(列C)。如果测试解析为True,我希望依次测试每个值并写入该行的(列d) 到目前为止,我的代码是: from __future__ import print_function import httplib2 import os from apiclient import discovery from oauth2client import client from oauth2client import tools from oa

我有一张谷歌表格,上面有学生的名字(列
a
B
),旁边的列中有一些值(列
C
)。如果测试解析为
True
,我希望依次测试每个值并写入该行的(列
d

到目前为止,我的代码是:

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

# Don't know what this is for
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'

# just copied code from Google's Dev guide
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

# PROBLEM IS IN THIS METHOD
def main():
    # Hope Google doesn't change all this API stuff and make us redo:
    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)
    # Fake URI of our sheet
    spreadsheetId = '5lksdfusdlfkjkj886kJUNKssdff'

    # retrieve data works
    rangeName = 'Sheet1!A2:D70'
    result = service.spreadsheets().values().get(
        spreadsheetId=spreadsheetId, range=rangeName).execute()
    values = result.get('values', [])

    if not values:
        print('No data found.')
    else:
        for row in values:
            print('Checking on: %s %s' % (row[0], row[1]))
            # PROBLEM... can't seem 
            if row[2] == "Some arbitrary condition":
                # Even trying to hardcode a range doesn't work 
                # I'd like to reference the coordinates of this row 
                result = service.spreadsheets().values().update(
                    spreadsheetId=spreadsheetId, range='Sheet1!D64',
                    body="can you see me?").execute()
                print(result)


if __name__ == '__main__':
    main()

你能提供你得到的stacktrace/error打印输出吗?
回溯(最近一次调用):文件“/Users/myUsername/PycharmProjects/pitendance/main.py”,第91行,在main()文件中“/Users/myUsername/PycharmProjects/pitendance/main.py”,第77行,在主打印中('检查:%s%s'(第[0]行,第[1]行))索引器错误:列出索引超出范围
尝试调试主函数中最后一个for循环。
索引器
表示您正试图访问
列表中不存在的项目。尝试在循环之前打印行,看看它是否给出了您期望的结果。奇怪的是,在控制台中输出成功,我在前两列中看到了所有名称。我的
result=service.spreadsheets().values().update(spreadsheetId=spreadsheetId,range='Sheet1!D64',body=“你能看见我吗?”)有些愚蠢。execute()