如何解决错误http 400“无法解析范围”?

时间:2019-04-23 06:23:33

标签: python google-sheets

我正在尝试将python连接到Google表格。我遵循了python快速入门页:https://developers.google.com/sheets/api/quickstart/python

我想用python写入我的Google工作表,但出现错误

我得到的错误是

  

https://sheets.googleapis.com/v4/spreadsheets/1VToTIofV5MNKJa-9nZ0J1r6CmRqynV361_xYHCxdsSA/values/Sheet%201%21A4%3AZ?valueInputOption=RAW&alt=json返回“无法解析范围:工作表1!A4:Z” >

这是一张新的空白纸

这是我的代码

from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
import datetime
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
BOOKING_SPREADSHEET_ID = '1VToTIofV5MNKJa-9nZ0J1r6CmRqynV361_xYHCxdsSA'
def get_service():
    """
    """
    creds = None
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server()
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)
    service = build('sheets', 'v4', credentials=creds)
    return service
def write_values():
    service = get_service()

    values = [
        [
            'Yes', 'No', 'Yes'
        ],
        [
            'Yes', 'No', 'Yes'
        ]
    ]
    body = {
        'values': values
    }

    # sheet_name = '15/4/2019'
    sheet_name = 'Sheet 1'
    range_values = sheet_name + '!A4:Z'

    result = service.spreadsheets().values().update(
        spreadsheetId=BOOKING_SPREADSHEET_ID, range=range_values,
        valueInputOption='RAW', body=body).execute()
    print('{0} cells updated.'.format(result.get('updatedCells')))
if __name__ == '__main__':
     write_values()
    ##test()

here是我的文档的链接

1 个答案:

答案 0 :(得分:0)

有同样的问题。通过更改此方法解决了

 sheet_name = 'Sheet 1'
    range_values = sheet_name + '!A4:Z'

range_values = "'Sheet 1'!A4:Z"

希望这对解决此问题的人有帮助