【问题标题】:How to fix the error http 400 "unable to parse range"?如何修复错误http 400“无法解析范围”?
【发布时间】:2019-09-12 07:06:44
【问题描述】:

我正在尝试将 python 连接到 Google Sheet。我关注了 python 快速入门页面:https://developers.google.com/sheets/api/quickstart/python

我想用 python 写到我的谷歌表,但是出错了

我得到的错误是

https://sheets.googleapis.com/v4/spreadsheets/1VToTIofV5MNKJa-9nZ0J1r6CmRqynV361_xYHCxdsSA/values/Sheet%201%21A4%3AZ?valueInputOption=RAW&alt=json 返回“无法解析范围:Sheet 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 是我的文档的链接

【问题讨论】:

  • 您的文档链接不公开

标签: python google-sheets


【解决方案1】:

有同样的问题。通过改变这个来解决它

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

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

希望这可以帮助解决此问题的任何人

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-11
    相关资源
    最近更新 更多