更新Google电子表格单元格(python) [英] Update Google spreadsheet cell (python)
问题描述
我阅读(相当)类似的帖子,但是我无法解决我的问题,并且Google文档没有100%有用,因为它们没有提供真实的示例.
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.
问题是我遇到了Google表格问题.我想做的是更新Google工作表中的单元格.基本上,我阅读了一张纸,如果特定的列没有特定的单词/状态,我将使用硒进行自动化(由于不重要,因此我不向您显示此步骤),完成后,我将编写Google工作表中的一列中的确定"一词.该列为空或在其上写有"OK"字样.我能够阅读工作表并进行自动化,但是在尝试更新工作表时出现错误.
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.
这是我的代码(基本上是Google的代码):
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')
这是我得到的错误:
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"'}]}]">
此行中有错误
value_range_body = {'values': values,
'majorDimension' : 'COLUMNS'}
尝试了不同的配置",但不知道如何解决,看起来不错.
tried different 'configurations' don't know how to fix it, it looks fine.
提前谢谢!
推荐答案
此修改如何?
在这种情况下,请对values
使用2维数组.我认为您的错误消息的原因是由于此.因此,请修改以下内容,然后再次对其进行测试.
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']]
参考:
- 方法:sheets.values.update
- Method: spreadsheets.values.update
Reference:
这篇关于更新Google电子表格单元格(python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!