我正在编写一个python程序,该程序从网站获取数据并将其分类到Google表格中的不同工作表中。该程序在添加和删除较小数量的行时有效,但是当我尝试插入或删除较大数量的行时,我达到了google api配额限制。
从我收集的信息中,我认为解决方案将是使用sheets.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尽管我对其进行了编辑以删除个人信息。这是我要进入Google工作表http://www.filedropper.com/gsheetsoutputexample的输出示例,所有交易都记录在主工作表中,但是如果其中一个辅助工作表的标题显示在说明中(csv的第5列)对于交易,该交易数据的副本也被放置在相应的工作表中。如果在事务描述中显示2个或更多工作表标题(或不显示),则将该事务的副本发送到冲突工作表。如果Google Sheets的配额是无限的,那么我的代码将按照所描述的方式运行,而不必担心被打扰。
编辑2:
1。)我想做的是检查“ E”列的值,如果其中一个工作表的标题是“ E”列的值的子字符串,则程序会将行追加到指定的工作表。因此,在这种情况下,“食物”,“食物!”和“我爱食物”的值都将附加到食物工作表中。
2。)工作表名称不是恒定的。我正在构建的程序供我的朋友使用,所以我做到了,这样您就可以通过gui将命名的工作表添加到电子表格中,以便他们可以创建自己的类别来过滤其数据。让我知道您是否还有其他问题,或者我不够清楚
编辑3:
在上面的代码中添加了注释
答案 0 :(得分:0)
这样一来,您不必等待很长时间,这里是Google Sheets API Doc的熟人,同时我将努力为您的特定情况创建解决方案。
使用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.