Task: I have a dataframe and I would like to append that data to a Google Sheet. The sheet has exactly the same columns as the DF and has exisiting data which I don't want to overwrite, but just add rows at the end of the sheet using the values from the DF.
Tried so far:
- I've followed the documentation on gspread using this code:
import json
df2.to_json(orient = 'columns')
values = df2.to_json(orient = 'columns')
wks.append_row (values, value_input_option='USER_ENTERED')
Result: APIError: { "error": { "code": 400, "message": "Invalid value at 'data.values[0]' (type.googleapis.com/google.protobuf.ListValue),
- I've followed the documentation on Google's API reference: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append
!pip install --upgrade google-api-python-client
from pprint import pprint
from googleapiclient import discovery
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/spreadsheets']
credentials = ServiceAccountCredentials.from_json_keyfile_name('NAME.json', scope)
gc = gspread.authorize(credentials)
wks = gc.open("SHEETNAME").sheet1
service = discovery.build(wks,'v1', credentials)
spreadsheet_id = 'MYID'
value_input_option = INSERT_ROWS
values = df2.to_json(orient = 'columns')
insert_data_option = values
request = service.spreadsheets().values().append(spreadsheetId=spreadsheet_id, range=range_, valueInputOption=value_input_option, insertDataOption=insert_data_option, body=value_range_body)
response = request.execute()
pprint(response)
Result: AttributeError: 'ServiceAccountCredentials' object has no attribute 'request'
Question: Is either of these solutions the right approach and how can I fix the errors? Or is there a totally different option that is much easier?
Thank you!