微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

GoogleAPI电子表格重复

如何解决GoogleAPI电子表格重复

我想知道怎么回事?

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

失败的{'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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。