Google表格API BatchUpdate InsertRange在Python 3.6中返回“收到的无效JSON有效负载。未知名称”

时间:2017-04-26 02:36:28

标签: python json syntax google-api google-sheets-api

尝试拨打Google表格API。我对电子表格请求的阅读工作正常,但以下失败:

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()

我被告知语法存在问题,而且之前无法找到JSON名称。

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: {

我做错了什么?这是一个学校项目。

1 个答案:

答案 0 :(得分:0)

尝试按照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)

如上所述,@ M.Leung检查你的第一行代码。您还可以查看Samples,这提供了使用Google表格API的示例代码实现。

希望这有帮助。