Google Sheets API Wont Let Me Write Data into My Google Sheet

Question:

Code is below. this isnt the full code but the basis of it. Im trying to take data from Twitter’s API and Write it to my Google Sheets API. Below is the Code.

from googleapiclient import discovery
from google.oauth2 import service_account
from google.oauth2.credentials import Credentials 
from googleapiclient.discovery import build


SERVICE_ACCOUNT_FILE = 'twitter.json' #json File should be in the same folder as this Python Script.
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']

creds = None
creds = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)

# The ID and range of a sample spreadsheet.
SAMPLE_SPREADSHEET_ID = '1f4iqVHljytmRSC2EZcApEiA2lE1cHa1o6Fr4s3t0AKg' 
#SAMPLE_RANGE_NAME = 'Class Data!A2:A60'

service = build('sheets', 'v4', credentials=creds)
sheet = service.spreadsheets()

# Call the Sheets API
result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
                                   range='twitterData!A1:A180').execute()
#ERROR HERE BELOW
request = sheet.values().update(spreadsheetId = SAMPLE_SPREADSHEET_ID,
                                  range = 'twitterData!B2:B180', valueInputOption='USER_ENTERED', body={"values": 8}).execute()

the one Line im getting an Error for has the comment above it Error Below. and this is the Error Message:

Traceback (most recent call last):
  File "C:UsersJohn DoeDesktopCodePythonTwittertest.py", line 27, in <module>
    request = sheet.values().update(spreadsheetId = SAMPLE_SPREADSHEET_ID,
  File "C:UsersJohn DoeAppDataLocalProgramsPythonPython38-32libsite-packagesgoogleapiclient_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "C:UsersJohn DoeAppDataLocalProgramsPythonPython38-32libsite-packagesgoogleapiclienthttp.py", line 938, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://sheets.googleapis.com/v4/spreadsheets/1f4iqVHljytmRSC2EZcApEiA2lE1cHa1o6Fr4s3t0AKg/values/twitterData%21B2%3AB180?valueInputOption=USER_ENTERED&alt=json returned "Invalid value at 'data.values' (type.googleapis.com/google.protobuf.ListValue), 8". Details: "[{'@type': 'type.googleapis.com/google.rpc.BadRequest', 'fieldViolations': [{'field': 'data.values', 'description': "Invalid value at 'data.values' (type.googleapis.com/google.protobuf.ListValue), 8"}]}]">

Ive used Google Sheets API before. Im not sure why this code isnt working. Its just that one line.

Asked By: Eric Byam

||

Answers:

I think that the reason for your current issue of "Invalid value at 'data.values' (type.googleapis.com/google.protobuf.ListValue), 8" is due to body={"values": 8}. In this case, it is required to use a 2-dimensional array. So, please modify it as follows.

From:

request = sheet.values().update(spreadsheetId = SAMPLE_SPREADSHEET_ID,
                                  range = 'twitterData!B2:B180', valueInputOption='USER_ENTERED', body={"values": 8}).execute()

To:

request = sheet.values().update(spreadsheetId = SAMPLE_SPREADSHEET_ID, range = 'twitterData!B2:B180', valueInputOption='USER_ENTERED', body={"values": [[8]]}).execute()
  • In this modification, body={"values": 8} is modified to body={"values": [[8]]}.
  • By this modification, 8 is put to the cell "B2" of "twitterData" sheet.

Reference:

Answered By: Tanaike