Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.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更新电子表格单元格值_Python_Api_Google Sheets Api - Fatal编程技术网

使用Python更新电子表格单元格值

使用Python更新电子表格单元格值,python,api,google-sheets-api,Python,Api,Google Sheets Api,我正在尝试用Python更新一个值,下面是我的代码: from googleapiclient.discovery import build from google_auth_oauthlib.flow import InstalledAppFlow from google.auth.transport.requests import Request global values global sheet global SAMPLE_SPREADSHEET_ID global SAMPLE_RA

我正在尝试用Python更新一个值,下面是我的代码:

from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

global values
global sheet
global SAMPLE_SPREADSHEET_ID
global SAMPLE_RANGE_NAME

def connexion():
    # Some code ...
    # Call the Sheets API
    return service.spreadsheets()

def update(values, sheet, SAMPLE_SPREADSHEET_ID, SAMPLE_RANGE_NAME):
    body = {
        'values': values
    }
    result = sheet.values().update(
    spreadsheetId=SAMPLE_SPREADSHEET_ID, range=SAMPLE_RANGE_NAME, valueInputOption='USER_ENTERED', body=body).execute()
    if str(result.get('updatedCells')) != 0:
        return True
    return False

sheet = connexion()
values = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID, range=SAMPLE_RANGE_NAME).execute().get('values', [])
for line in values:
    if line[3] == 'Hi !':
        line[5] == 'hello'
if update(values, sheet, SAMPLE_SPREADSHEET_ID, SAMPLE_RANGE_NAME):
    print('   Update ok')
else:
    print('   Error')

我没有任何错误,但已打印“更新确定”。但是,第6个细胞系[5]中的值未更新。。。请问我怎么做?谢谢

这次修改怎么样

当脚本中的
sheet
变量可用于获取值并将值放入Google电子表格时,我认为您的most脚本是正确的。我认为只有一个修改点。将
行[5]
的值与
行[5]==“hello”
处的
hello
进行比较。这样,
hello
就不会放到
行[5]
。我认为你的问题的原因是这样的。因此,请修改如下

发件人: 致: 注:
  • 在此修改中,它假设从
    sheet.values().get()
    检索到的
    values
    列多于6列

如果这不是直接的解决方案,我很抱歉。

@tooioi感谢您的回复。我很高兴你的问题解决了。
line[5] == 'hello'
line[5] = 'hello'