如何将大量行追加到Google工作表中而不必查看API Qu

2024-10-02 22:27:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个python程序,它从一个网站获取数据,并在Google工作表中将其分类到不同的工作表中。这个程序在附加和删除较小数量的行时工作,但当我试图插入或删除大量行时,我达到了googleapi配额限制。在

据我所知,我相信解决办法是使用spreadsheets.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配额

^{pr2}$

如果你能帮我把它变成一个批量更新程序,我将不胜感激

编辑:

这里有一个csv的例子,它是从我的venmo配置文件中获取的。http://www.filedropper.com/csvexample虽然我编辑了它以删除个人信息。下面是一个输出的例子,我想把所有的事务都放到google sheetshttp://www.filedropper.com/gsheetsoutputexample中,但是如果一个辅助工作表的标题出现在事务的描述(csv的第5列)中,则该事务数据的副本也会放在相应的工作表中。如果一个事务的描述中显示了两个或多个工作表标题(或没有),则会将该事务的副本发送到冲突工作表。如果googlesheets配额是无限的,那么我的代码就可以按照所描述的方式运行,而不必担心中断。在

编辑2:

1.)我要做的是检查列“E”的值,如果其中一个工作表的标题是列“E”值的子字符串,则程序会将该行追加到指定的工作表中。所以在这个例子中,价值观是“食物”,“食物!”,和“我爱食物”都会被附加到食物工作表中。在

2.)工作表名称不是常量。我正在构建的程序是供我的朋友使用的,所以我做了这个程序,这样你就可以通过一个gui将命名的工作表添加到电子表格中,这样他们就可以创建自己的类别来过滤数据。如果你还有其他问题,或者我没有很好地澄清,请告诉我

编辑3:

在上面的代码中添加了注释


Tags: csvtheto数据in程序ifis
1条回答
网友
1楼 · 发布于 2024-10-02 22:27:17

所以你不必等待太久,这里是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.

相关问题 更多 >