Reputation: 131
I'm trying to update a value in Python, here is my code :
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')
I do not have any error but "Update ok" is printed. However, the value in the 6th cell line[5] is not updated... How can I do this please ? Thanks !
Upvotes: 1
Views: 1727
Reputation: 201388
When the variable of sheet
in your script can be used for getting and putting values to Google Spreadsheet, I think that your most script is correct. I think that there is only one modification point. The value of line[5]
is compared with hello
at line[5] == 'hello'
. By this, hello
is not put to line[5]
. I think that the reason of your issue is this. So please modify as follows.
line[5] == 'hello'
line[5] = 'hello'
values
retrieved from sheet.values().get()
is more than 6.Upvotes: 1