2017-04-26 86 views
0

試圖撥打Google Sheets API。我的電子表格要求的閱讀工作,但下面的失敗:Google Sheets API BatchUpdate InsertRange在Python 3.6中返回「Invalid JSON payload received。Unknown name」

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

我在做什麼錯?這是一個學校項目。

+2

你發佈的第一行是'resource'作爲你的請求體的關鍵嗎?應該是{'resource':xxx}或沒有'resource',只需{'request':xxx} –

回答

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 Sheets API的示例代碼實現。

希望這會有所幫助。

相關問題