python - 如何在不超过 API 配额的情况下将大量行附加到 Google 表格
问题描述
我正在编写一个 python 程序,它从网站获取数据并将其分类到 Google 表格中的不同工作表中。该程序在添加和删除少量行时有效,但是当我尝试插入或删除大量行时,我达到了 google api 配额限制。
据我所知,我相信解决方案是使用电子表格.values.batchUpdate() 方法,因为从我收集的内容来看,它会立即发送所有请求,然后一旦它们被验证为对特定工作表的有效请求,它们都是一次执行。不幸的是,没有调用此方法的 gspread 函数,我目前在尝试使用原始 api 时迷路了。
这就是我附加数据的方式
sheet = vt.sheet1
........
with open('objectbuffer.csv', encoding='utf-8') as bfile:
reader = csv.reader(bfile, delimiter=',')
#gets every worksheet in the spreadsheet
wkshts = vt.worksheets()
#each row in the csv is evaluated for
#and is copied into any corresponding worksheet one at a time
for row in reader:
kwsFound = 0
hasUploaded = False
appendRow = [row[0],row[1],row[2],row[3],row[4],row[5]]
#iterates through every dynamically created worksheet in the spreadsheet
for sheets in wkshts:
#if the title of the sheet is found anywhere within Column "E"
#then 1 is added to the number of keywords found
if sheets.title in row[4]:
kwsFound += 1
kwSheet = sheets
#if only one keyword (title of a worksheet) is found ANYWHERE in the row
#then that row is copied to the worksheet with the name found
if kwsFound == 1:
kwSheet.append_row(appendRow,"USER_ENTERED")
hasUploaded = True
#if no keyword is found/ more than 1 is found
#the row is copied to the conflicts worksheet (which is constant)
if hasUploaded == False:
conflicts.append_row(appendRow,"USER_ENTERED")
#every row is always copied to the main worksheet
sheet.append_row(appendRow,"USER_ENTERED")
kwsFound/kwsSheet 将数据分类到单独的工作表中。目前,gspread append_row 函数是我用来一次追加数据 1 的函数,这使我超过了 api 限制。
奖金问题
这就是我在程序中删除重复行的方式。由于删除请求一次发送 1 个,这也使程序超出了 api 配额
allVal = sheet.get_all_values()
rowTot = len(sheet.get_all_values())
standard = ""
counter = 0
dcounter = 0
deleteRows = []
while counter<rowTot:
if allVal[counter] == standard:
deleteRows.append(counter)
else:
standard = allVal[counter]
counter+=1
while dcounter < len(deleteRows) :
sheet.delete_row(deleteRows[dcounter]-dcounter)
sleep(.5)
dcounter+=1
将不胜感激帮助将其制作成batchUpdate
编辑:
这是通过抓取我的 venmo 配置文件生成的 csv 示例。http://www.filedropper.com/csvexample虽然我对其进行了编辑以删除个人信息。这是我想进入谷歌表格的输出示例http://www.filedropper.com/gsheetsoutputexample,所有交易都在主工作表上,但如果其中一个辅助工作表的标题出现在交易的描述(csv 的第 5 列)中,则该交易数据的副本也将放置在相应的工作表中。如果交易描述中出现 2 个或更多工作表标题(或没有),则该交易的副本将发送到冲突工作表。如果 google sheet 配额是无限的,那么我的代码将以所描述的方式运行,而不必担心中断。
编辑2:
1.) 我要做的是检查“E”列的值,如果其中一个工作表的标题是“E”列值的子字符串,则程序将该行附加到指定的工作表。所以在这种情况下,“食物”、“食物!”和“我爱食物”的值都将附加到食物工作表中。
2.) 工作表名称不是恒定的。我正在构建的程序是供我的朋友使用的,所以我这样做是为了让您通过 gui 将命名的工作表添加到电子表格中,这样他们就可以创建自己的类别来过滤他们的数据。让我知道您是否有其他问题,或者我的解释不够清楚
编辑 3:
在上面的代码中添加了注释
解决方案
为了让您不必等待很长时间,这里是Google Sheets API Doc Get acquainted with it,与此同时,我将努力为您的具体情况创建解决方案。
使用 gspread 执行此操作,请参阅doc
尝试这样的事情:
#!/usr/bin/python3
#This dict represents the request body for batchUpdate(body)
thisDict = {
"requests": [
{
#we append update commands to a list and then place that list here. Next we send thisDict as the body of a batchupdate.
}
],
"includeSpreadsheetInResponse": bool, #todo set this to bool value
"responseRanges": [
string #todo set this is string range
],
"responseIncludeGridData": bool #todo set this to bool value
}
#to contain our request objects
List = []
with open('objectbuffer.csv', encoding='utf-8') as bfile:
reader = csv.reader(bfile, delimiter=',')
wkshts = vt.worksheets()
for row in reader:
kwsFound = 0
hasUploaded = False
appendRow = [row[0],row[1],row[2],row[3],row[4],row[5]]
for sheets in wkshts:
if sheets.title in row[4]:
kwsFound += 1
kwSheet = sheets
if kwsFound == 1:
List.append("kwSheet.append_row(appendRow,'USER_ENTERED')") #append command to list
hasUploaded = True
if hasUploaded == False:
List.append("conflicts.append_row(appendRow,'USER_ENTERED')") #append command to list
List.append("sheet.append_row(appendRow,'USER_ENTERED')") #append command to list
thisDict["requests"] = List #set requests equal to the list of commands
spreadsheets.batchUpdate(thisDict) #send the request body with a list of command to execute.
推荐阅读
- html - 选中的复选框不会改变 CSS
- android - 如何使用两个recyclerview(一个垂直,另一个水平)
- django - Heroku 和 Django 2.x ModuleNotFoundError:没有名为“rest_auth”的模块
- python - 为什么我的数据框还是空的?
- amazon-web-services - 如何将特殊数据附加到 AWS SES 电子邮件发送?
- loops - Ionic 3 *ngFor 在数组元素内
- java - 从 firebase 在导航栏中加载个人资料图片和用户名
- sql - 字符串或二进制数据将在 INSERT 语句中被截断
- r - 在相同的 data.tables 上执行 bind_rows() 和 rbind() 后,相同()= FALSE?
- php - 带有like子句mysql的FIND_IN_SET