GoogleAPI电子表格重复

时间:2020-11-12 11:02:40

标签: python excel google-chrome automation google-drive-api

我想知道怎么回事?

我每天早上在服务器中运行此代码,以将数据备份到电子表格中。 该代码通常在第二天失败。我试图抓住它并删除重复的页面,但不知道发生了什么。错误如下...

失败的{'code':400,'message':'无效的请求[0] .addSheet:名称为“ 2020-11-12”的工作表已存在。请输入其他名称。','状态':'INVALID_ARGUMENT'}

import gspread
from gspread_dataframe import set_with_dataframe
from oauth2client.service_account import  ServiceAccountCredentials

def goog_formatSlide(goog_spread, goog_slide):
        gc = goog_getpermission()
        sh = gc.open(goog_spread)
        ws = sh.worksheet(goog_slide)
    
        ws.update_acell("J4", "ABC")


def goog_uploadDataFrame(df, goog_spread): #goog_spread = googledrive_spreadsheet_name
    run = False 
    while not run: #while True
        try:
            A_pd = df
            gc = goog_getpermission() #authen the permission to access the drive
            sh = gc.open(goog_spread) #open drive_spreadsheet
            worksheet_name = goog_worksheet_name() #assign a name
            try:
                ws = sh.add_worksheet(title=str(worksheet_name), rows="500", cols="500") # add a new sheet 
                set_with_dataframe(ws, warrant_pd, row = 4, col =1 , include_index= True, include_column_header=True) # put the dataframe in the sheet
                goog_formatSlide(goog_spread, str(worksheet_name))
                run = True # assign format to the sheet

            except Exception as f: # if name is assigned before 
                print("Failed " + str(f))
                ws = sh.worksheet(str(worksheet_name)) # find out the duplicated sheet 
                sh.del_worksheet(ws) #delate the duplicated sheet 
                continue # do it again to make a new sheet with the same name 

        except Exception as e:
            print("Failed " + str(e))
            continue

0 个答案:

没有答案