evank28
evank28

Reputation: 139

Google Sheets API BatchUpdate InsertRange returns "Invalid JSON payload received. Unknown name" in Python 3.6

Trying to make a call to a Google Sheets API. My reading of the spreadsheet requests work but the following fails:

batch_update_spreadsheet_request_body = resource: {
               'requests': ["insertRange": { 'range':{
                                            "sheetId": 1034566956,
                                                "startRowIndex": rowToInsert,
                                                "endRowIndex": rowToInsert,
                                                "startColumnIndex": 0,
                                                "endColumnIndex": 12, },
                     #'shiftDimension': discovery.ROWS,
                                    }],
        "includeSpreadsheetInResponse":False,
          "responseRanges": False,
          "responseIncludeGridData": False,         }
request = service.spreadsheets().batchUpdate(spreadsheetId=spreadsheetId, body=batch_update_spreadsheet_request_body)
response = request.execute()

I am told there is an issue both with the syntax, and earlier that the JSON names could not be found.

Syntax Error: invalid syntax: C:\Users\evank\Google Drive\M4\ComputerScience\Create Project\Quickstart.py, line 218, pos 52 batch_update_spreadsheet_request_body = resource: {

What am I doing wrong? This is for a school project.

Upvotes: 1

Views: 1585

Answers (1)

Mr.Rebot
Mr.Rebot

Reputation: 6791

Try following the sample code of Method: spreadsheets.batchUpdate

"""
BEFORE RUNNING:
---------------
1. If not already done, enable the Google Sheets API
   and check the quota for your project at
   https://console.developers.google.com/apis/api/sheets
2. Install the Python client library for Google APIs by running
   `pip install --upgrade google-api-python-client`
"""
from pprint import pprint

from googleapiclient import discovery

# TODO: Change placeholder below to generate authentication credentials. See
# https://developers.google.com/sheets/quickstart/python#step_3_set_up_the_sample
#
# Authorize using one of the following scopes:
#     'https://www.googleapis.com/auth/drive'
#     'https://www.googleapis.com/auth/spreadsheets'
credentials = None

service = discovery.build('sheets', 'v4', credentials=credentials)

# The spreadsheet to apply the updates to.
spreadsheet_id = ''  # TODO: Update placeholder value.

batch_update_spreadsheet_request_body = {
    # A list of updates to apply to the spreadsheet.
    # Requests will be applied in the order they are specified.
    # If any request is not valid, no requests will be applied.
    'requests': [],  # TODO: Update placeholder value.

    # TODO: Add desired entries to the request body.
}

request = service.spreadsheets().batchUpdate(spreadsheetId=spreadsheet_id, body=batch_update_spreadsheet_request_body)
response = request.execute()

# TODO: Change code below to process the `response` dict:
pprint(response)

As stated, @M.Leung check you first line of code. You can also check the Samples, this provides sample code implementation for using Google Sheets API.

Hope this helps.

Upvotes: 1

Related Questions