使用Python更新电子表格单元格值
我正在尝试用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
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()
列多于6列values
如果这不是直接的解决方案,我很抱歉。@tooioi感谢您的回复。我很高兴你的问题解决了。
line[5] == 'hello'
line[5] = 'hello'