首页 > 解决方案 > GoogleAPI 电子表格重复

问题描述

我想知道出了什么问题?

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

失败 {'code': 400, 'message': 'Invalid requests[0].addSheet: 名称为“2020-11-12”的工作表已存在。请输入其他名称。', 'status': '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

标签: pythonexcelgoogle-chromeautomationgoogle-drive-api

解决方案


推荐阅读