python - 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
解决方案
推荐阅读
- scala - 如何通过 akka/alpakka 流传输 parquet 文件目录
- r - 在R中绘制地图,但它什么也没显示
- google-chrome-devtools - DevTools 中的屏幕尺寸与实际设备尺寸不匹配
- android - Zebra ZQ620 ^FO 命令不移动原点
- c - 如何在 WINAPI GDI 中制作淡入淡出效果?
- excel - 虽然语句返回应用程序/对象定义的错误“错误 1004”
- pytorch - PyTorch 训练在缓存图像后退出
- python - 在 cx_freeze 中排除 PyQT5,使库更小,exe 启动更快
- .net - 如何允许 ABP 在 .NET MVC 项目中检索您的计算机 ID
- javascript - 在 vue 中使用爱普生 SDK