在不覆盖现有Google工作表的情况下更新Google工作表

2024-04-26 15:15:05 发布

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

我正在将一个数据框写入谷歌表单

app=Flask(__name__)

@app.route('/',methods=['POST'])
def rp_webhook():
    if request.method == 'POST':
        results= request.json
        x=json.dumps(results)
        y=json.loads(x)

df=pd.DataFrame()
data=[{'Contact':(y['contact']['urn']),
     "Created_on":(y['input']['created_on']),
     "Message":(y['input']['text']),
     "Flow_name":(y['flow']['name'])},]
danger_data=pd.DataFrame.from_records(data,index=None,columns['Contact','Created_on','Message','Flow_name'])
df=df.append(danger_data) 
def write_to_googlesheet():
            credentials_file = "onlygsheets-8356dda396762.json"
            scope = ['https://spreadsheets.google.com/feeds',
                     'https://www.googleapis.com/auth/drive']
            credentials = ServiceAccountCredentials.from_json_keyfile_name(credentials_file, scope)
            gc=gspread.authorize(credentials)


            danger_wb=gc.open_by_key('1PGKGPA90If8956fBgfii6w_Hn1Y9lO_rfs-eKBKDD2Fp').sheet1
            set_with_dataframe(danger_wb,df)

            print(df)


        write_to_googlesheet()

此代码正在覆盖google工作表中的数据。如何在不覆盖现有数据帧的情况下更新数据


Tags: 数据namejsonappdataframedfdataon