【问题标题】:Trouble implementing batch update rows while writing to a google sheet using gspread使用 gspread 写入 Google 工作表时无法实现批量更新行
【发布时间】:2022-01-18 04:19:09
【问题描述】:

我正在尝试在使用 gspread 库将数据写入谷歌电子表格时批量更新行。当以下函数get_content() 产生一个结果时,我可以完美地执行batch update rows

import gspread
import requests
from oauth2client.service_account import ServiceAccountCredentials

link = 'https://api.coinmarketcap.com/data-api/v3/cryptocurrency/listing'
params = {
    'start': 1,
    'limit': '200',
    'sortBy': 'market_cap',
    'sortType': 'desc',
    'convert': 'USD,BTC,ETH',
    'cryptoType': 'all',
    'tagType': 'all',
    'audited': 'false',
    'aux': 'ath,atl,high24h,low24h,num_market_pairs,cmc_rank,date_added,max_supply,circulating_supply,total_supply,volume_7d,volume_30d,self_reported_circulating_supply,self_reported_market_cap'
}

def authenticate():
    scope = [
        "https://spreadsheets.google.com/feeds",
        "https://www.googleapis.com/auth/spreadsheets",
        "https://www.googleapis.com/auth/drive.file",
        "https://www.googleapis.com/auth/drive"
    ]

    creds = ServiceAccountCredentials.from_json_keyfile_name("creds.json", scope)
    client = gspread.authorize(creds)
    return client

def get_content(s,link,params):
    res = s.get(link,params=params)
    for item in res.json()['data']['cryptoCurrencyList']:
        yield item['id']

if __name__ == '__main__':
    with requests.Session() as s:
        s.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36'
        client = authenticate()
        sheet = client.open("testFile").sheet1
        sheet.insert_row(['NAME'],1)
        item_list = []
        cell_list = sheet.range('A2:A201')
        for item in get_content(s,link,params):
            item_list.append(item)

        for i, val in enumerate(item_list):
            cell_list[i].value = val
        sheet.update_cells(cell_list)

但是,当同一个函数get_content() 产生多个结果时,我找不到任何方法来执行batch update rows。这次的范围是sheet.range('A2:D201')

def get_content(s,link,params):
    res = s.get(link,params=params)
    for item in res.json()['data']['cryptoCurrencyList']:
        yield item['id'],item['name'],item['symbol'],item['slug']

如何在使用 gspread 写入谷歌表格时批量更新行?

【问题讨论】:

    标签: python web-scraping google-sheets google-sheets-api gspread


    【解决方案1】:

    你的情况,下面的修改怎么样?

    发件人:

    item_list = []
    cell_list = sheet.range('A2:A201')
    for item in get_content(s,link,params):
        item_list.append(item)
    
    for i, val in enumerate(item_list):
        cell_list[i].value = val
    sheet.update_cells(cell_list)
    

    收件人:

    item_list = []
    for item in get_content(s,link,params):
        item_list.append(item)
    
    sheet.update('A2:D201', item_list, value_input_option='USER_ENTERED')
    

    参考:

    【讨论】:

    • 它做得非常好。但是,要使其正常工作,我必须先升级 gspread 库。感谢一万亿@Tanaike。
    • @MITHU 感谢您的回复。我很高兴你的问题得到了解决。也谢谢你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-03
    • 1970-01-01
    相关资源
    最近更新 更多