Update app.py
Browse files
app.py
CHANGED
@@ -395,6 +395,30 @@ template_sheet_id = '1i5mrmlTs5sPWtx2mBtc_f-zm1D3x21r1T_77guki8_8'
|
|
395 |
@app.post("/edit-spreadsheet")
|
396 |
async def edit_spreadsheet(target_sheet_id: str = Form(...)):
|
397 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
398 |
# Retrieve the template spreadsheet's metadata
|
399 |
template_spreadsheet = sheet_service.spreadsheets().get(spreadsheetId=template_sheet_id).execute()
|
400 |
|
@@ -410,7 +434,28 @@ async def edit_spreadsheet(target_sheet_id: str = Form(...)):
|
|
410 |
)
|
411 |
copy_response = copy_request.execute()
|
412 |
|
413 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
414 |
|
415 |
logging.info(f"All sheets from template {template_sheet_id} copied to {target_sheet_id}.")
|
416 |
return {"message": "Spreadsheet copied successfully"}
|
|
|
395 |
@app.post("/edit-spreadsheet")
|
396 |
async def edit_spreadsheet(target_sheet_id: str = Form(...)):
|
397 |
try:
|
398 |
+
# Retrieve the target spreadsheet's metadata
|
399 |
+
target_spreadsheet = sheet_service.spreadsheets().get(spreadsheetId=target_sheet_id).execute()
|
400 |
+
|
401 |
+
# Collect all sheet IDs in the target spreadsheet
|
402 |
+
target_sheet_ids = [sheet['properties']['sheetId'] for sheet in target_spreadsheet.get('sheets', [])]
|
403 |
+
|
404 |
+
# Prepare batch update request to delete existing sheets
|
405 |
+
requests = []
|
406 |
+
for sheet_id in target_sheet_ids:
|
407 |
+
requests.append({
|
408 |
+
"deleteSheet": {
|
409 |
+
"sheetId": sheet_id
|
410 |
+
}
|
411 |
+
})
|
412 |
+
|
413 |
+
if requests:
|
414 |
+
# Execute batch update to delete sheets
|
415 |
+
batch_update_request = {"requests": requests}
|
416 |
+
sheet_service.spreadsheets().batchUpdate(
|
417 |
+
spreadsheetId=target_sheet_id,
|
418 |
+
body=batch_update_request
|
419 |
+
).execute()
|
420 |
+
logging.info(f"Deleted existing sheets in target spreadsheet {target_sheet_id}.")
|
421 |
+
|
422 |
# Retrieve the template spreadsheet's metadata
|
423 |
template_spreadsheet = sheet_service.spreadsheets().get(spreadsheetId=template_sheet_id).execute()
|
424 |
|
|
|
434 |
)
|
435 |
copy_response = copy_request.execute()
|
436 |
|
437 |
+
# Get the copied sheet ID
|
438 |
+
copied_sheet_id = copy_response['sheetId']
|
439 |
+
|
440 |
+
# Rename the copied sheet to remove "Copy of"
|
441 |
+
copied_sheet_title = sheet['properties']['title'].replace("Copy of ", "")
|
442 |
+
rename_request = {
|
443 |
+
"updateSheetProperties": {
|
444 |
+
"properties": {
|
445 |
+
"sheetId": copied_sheet_id,
|
446 |
+
"title": copied_sheet_title
|
447 |
+
},
|
448 |
+
"fields": "title"
|
449 |
+
}
|
450 |
+
}
|
451 |
+
|
452 |
+
# Execute rename request
|
453 |
+
sheet_service.spreadsheets().batchUpdate(
|
454 |
+
spreadsheetId=target_sheet_id,
|
455 |
+
body={"requests": [rename_request]}
|
456 |
+
).execute()
|
457 |
+
|
458 |
+
logging.info(f"Sheet {sheet['properties']['title']} copied and renamed to {copied_sheet_title} in {target_sheet_id}.")
|
459 |
|
460 |
logging.info(f"All sheets from template {template_sheet_id} copied to {target_sheet_id}.")
|
461 |
return {"message": "Spreadsheet copied successfully"}
|