I read (quite) similar posts like this but I'm not able to fix my problem, and google documentation is not 100% useful since they don't provide real examples.
The thing is I'm facing a problem with google sheets. What I'm trying to do is to update cells in a google sheet. Basically, I read a sheet and if a specific column doesn't have a specific word/state, I do an automation with selenium (I don't show you this step since it's not important), and once is done, I write the word 'OK' in a column in my Google sheet. This column is empty or with the word 'OK' wrote on it. I'm able to read the sheet and do the automation, but I get an error when trying to update the sheet.
This is my code (basically is Google's code):
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
SPREADSHEET_ID = '1cuT66AD3TBQ-1IWCisB0pUPzlw-iOuuU7vO2TqRj3bQ' #control seguimiento robot
WORKSHEET_NAME = 'PRUEBAS DAVID cambio estado TAS'
async def leerGoogleSheet():
creds = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
# pass
flow = InstalledAppFlow.from_client_secrets_file(
'client_secret.json', SCOPES)
creds = flow.run_local_server(port=0)
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('sheets', 'v4', credentials=creds)
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=SPREADSHEET_ID,
range=WORKSHEET_NAME).execute()
values = result.get('values', [])
print('result: ' + str(result))
print('range: ' + str(range))
print('values: ' + str(result['values']))
print('WO: ' + str(result['values'][1][1]))
if not values:
print('No data found.')
print(traceback)
else:
losDatos = []
for row in range(1, len(values)):
# print('num_fila:' +str(num_fila))
if 'TAS' in values[row][0]:
if values[row][11] == 'Pendiente':
try:
if values[row][12]:
print('ya tiene valor OK')
pass
except:
print('Hay TAS')
datos = []
datos.append(values[row][0]) #tas
datos.append(values[row][1]) #wo
datos.append(row)
losDatos.append(datos)
await escribirEnSheet(service, row)
else:
print('No hay TAS')
print(losDatos)
#CAMBIAR ESTADO
async def escribirEnSheet(service, row):
range_ = 'M'+str(row)
value_input_option = 'RAW' # TODO: Update placeholder value.
values = ['OK']
value_range_body = {'values': values,
'majorDimension' : 'COLUMNS'}
print('voy a hacer la request para escribir')
request = service.spreadsheets().values().update(spreadsheetId=SPREADSHEET_ID, range=range_, valueInputOption=value_input_option, body=value_range_body)
response = request.execute()
print('ya he escrito')
And this is the error I got:
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://sheets.googleapis.com/v4/spreadsheets/1cuT66AD3TBQ-1IWCisB0pUPzlw-iOuuU7vO2TqRj3bQ/values/M2?valueInputOption=RAW&alt=
json returned "Invalid value at 'data.values' (type.googleapis.com/google.protobuf.ListValue), "OK"". Details: "[{'@type': 'type.googleapis.com/google.rpc.BadRequest', 'fieldViolations': [{'f
ield': 'data.values', 'description': 'Invalid value at \'data.values\' (type.googleapis.com/google.protobuf.ListValue), "OK"'}]}]">
The error is in this line,
value_range_body = {'values': values,
'majorDimension' : 'COLUMNS'}
tried different 'configurations' don't know how to fix it, it looks fine.
Thanks in advance!
How about this modification?
In this case, please use 2 dimensional array for values. I think that the reason of your error message is due to this. So please modify as follows ans test it again.
values = ['OK']
values = [['OK']]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With