Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -391,8 +391,65 @@ async def check_user_doc(user_id: str = Form(...)):
|
|
| 391 |
})
|
| 392 |
return {"dbUserExists": False}
|
| 393 |
|
|
|
|
| 394 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 395 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
# image upload
|
| 397 |
# receive image id and user_id
|
| 398 |
# check if they have enough credits
|
|
|
|
| 391 |
})
|
| 392 |
return {"dbUserExists": False}
|
| 393 |
|
| 394 |
+
template_sheet_id = '1i5mrmlTs5sPWtx2mBtc_f-zm1D3x21r1T_77guki8_8'
|
| 395 |
|
| 396 |
+
@app.post("/edit-spreadsheet")
|
| 397 |
+
async def edit_spreadsheet(sheet_id: str = Form(...)):
|
| 398 |
+
try:
|
| 399 |
+
# Retrieve the template spreadsheet's metadata
|
| 400 |
+
template_spreadsheet = sheet_service.spreadsheets().get(spreadsheetId=template_sheet_id).execute()
|
| 401 |
+
|
| 402 |
+
# Iterate through each sheet in the template spreadsheet
|
| 403 |
+
for sheet in template_spreadsheet.get('sheets', []):
|
| 404 |
+
title = sheet['properties']['title']
|
| 405 |
+
sheet_id = sheet['properties']['sheetId']
|
| 406 |
+
|
| 407 |
+
# Get the data from the template sheet
|
| 408 |
+
template_sheet_data = sheet_service.spreadsheets().values().get(
|
| 409 |
+
spreadsheetId=template_sheet_id,
|
| 410 |
+
range=title
|
| 411 |
+
).execute().get('values', [])
|
| 412 |
+
|
| 413 |
+
# Check if the target spreadsheet has a sheet with the same title
|
| 414 |
+
try:
|
| 415 |
+
target_spreadsheet = sheet_service.spreadsheets().get(spreadsheetId=sheet_id).execute()
|
| 416 |
+
sheet_exists = any(s['properties']['title'] == title for s in target_spreadsheet.get('sheets', []))
|
| 417 |
+
|
| 418 |
+
if not sheet_exists:
|
| 419 |
+
# Add a new sheet to the target spreadsheet
|
| 420 |
+
requests = [{
|
| 421 |
+
'addSheet': {
|
| 422 |
+
'properties': {
|
| 423 |
+
'title': title,
|
| 424 |
+
'sheetId': sheet_id
|
| 425 |
+
}
|
| 426 |
+
}
|
| 427 |
+
}]
|
| 428 |
+
sheet_service.spreadsheets().batchUpdate(
|
| 429 |
+
spreadsheetId=sheet_id,
|
| 430 |
+
body={'requests': requests}
|
| 431 |
+
).execute()
|
| 432 |
+
|
| 433 |
+
# Update the target sheet with the data from the template sheet
|
| 434 |
+
sheet_service.spreadsheets().values().update(
|
| 435 |
+
spreadsheetId=sheet_id,
|
| 436 |
+
range=title,
|
| 437 |
+
valueInputOption='RAW',
|
| 438 |
+
body={'values': template_sheet_data}
|
| 439 |
+
).execute()
|
| 440 |
+
|
| 441 |
+
except Exception as e:
|
| 442 |
+
# Handle the case where the target spreadsheet is not found or another error occurs
|
| 443 |
+
logging.error(f"Error updating sheet {title} in spreadsheet {sheet_id}: {e}")
|
| 444 |
+
continue
|
| 445 |
+
|
| 446 |
+
logging.info(f"All sheets from template {template_sheet_id} copied to {sheet_id}.")
|
| 447 |
+
return {"message": "Spreadsheet copied successfully"}
|
| 448 |
|
| 449 |
+
except Exception as e:
|
| 450 |
+
logging.error(f"Failed to copy sheets: {e}")
|
| 451 |
+
return {"error": str(e)}
|
| 452 |
+
|
| 453 |
# image upload
|
| 454 |
# receive image id and user_id
|
| 455 |
# check if they have enough credits
|