Py学习  »  Python

python-google sheets api-不写入指定范围

Kerem Akagündüz • 4 年前 • 434 次点击  

我正在从网上抓取一些数据并将其写入谷歌电子表格。 经过长时间的逃避之后,我请求你的帮助。

用于此脚本的库:

from __future__ import print_function
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
import requests
from bs4 import BeautifulSoup
from datetime import date, timedelta

下面的代码是 for i in range(3) 循环。

即使我写信 range='Sheet1!E:G' 手动,它仍然从A列开始写入。

当我将工作表名称替换为另一个工作表名称时,它会将值附加到该特定工作表,但仍不使用指定的范围。我试过各种各样的射程,但还是不好。

    SCOPES = 'https://www.googleapis.com/auth/spreadsheets'
    spreadsheet_id = '*******'
    store = file.Storage('token.json')
    creds = store.get()
    if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
        creds = tools.run_flow(flow, store)
    service = build('sheets', 'v4', http=creds.authorize(Http()))

        values = [
            [
                max_temp, avg_temp, min_temp
            ]

        ]
        body = {
            'values': values
        }

        range_list = ['B:D','E:G','H:J']
        range_name = f'Sheet1!{range_list[i]}'

        sheet = service.spreadsheets()
        result = sheet.values().append(
            spreadsheetId=spreadsheet_id, range=range_name,
            valueInputOption='USER_ENTERED', body=body).execute()

救命啊!!!

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/46573
 
434 次点击  
文章 [ 1 ]  |  最新文章 4 年前