I am writing a python script that first creates a sheet in google sheets, then uploads a CSV into it, then generates tables for slides from the sheet. I am using sheets API, and I can successfully create a new sheet. However, when trying to upload the CSV into it, I get an "invalid JSON payload" error, saying that PasteDataRequest is not found. I'm taking it directly from the docs PasteDataRequest docs, batchUpdate docs
And this is how the script looks like
from __future__ import print_function
from apiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools
from datetime import datetime
SCOPES = (
'https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/presentations',
)
store = file.Storage('.//script//storage.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('.//script//credentials.json', SCOPES)
creds = tools.run_flow(flow, store)
HTTP = creds.authorize(Http())
SHEETS = discovery.build('sheets', 'v4', http=HTTP)
SLIDES = discovery.build('slides', 'v1', http=HTTP)
print('Creating Sheet')
csvInInbox = './/Inbox//Report.csv'
reportSheet = {
'properties': {
'title': 'CRM results report - ' + str(datetime.now())
}
}
sheetsRsp = SHEETS.spreadsheets().create(body=reportSheet,fields='spreadsheetId').execute()
sheetId = sheetsRsp.get('spreadsheetId')
print('DONE\nSpreadsheet ID: {0}'.format(sheetsRsp.get('spreadsheetId')))
print('\nUploading CSV into sheet ' + sheetId)
sheetReq = [{'PasteDataRequest': {
'coordinate': {
'sheetId': sheetId,
'rowIndex': 0,
'columnIndex': 0
},
'data': csvInInbox,
'type': "enum" "(PASTE_NORMAL)",
'delimiter': ",",
}
}]
sheetsRsp = SHEETS.spreadsheets().batchUpdate(body = {'requests': sheetReq},spreadsheetId=sheetId).execute()
print("DONE\n")
It seems that according to the docs, thats the correct request. Can anyone tell what am I doing wrong here? It's the first time I'm using APIs so maybe I forgot something.
Error message:
"Invalid JSON payload received. Unknown name "PasteDataRequest" at 'requests[0]': Cannot find field."