m8chaa commited on
Commit
0aab4d9
·
verified ·
1 Parent(s): 08d1b40

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -41
app.py CHANGED
@@ -394,56 +394,26 @@ async def check_user_doc(user_id: str = Form(...)):
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:
 
394
  template_sheet_id = '1i5mrmlTs5sPWtx2mBtc_f-zm1D3x21r1T_77guki8_8'
395
 
396
  @app.post("/edit-spreadsheet")
397
+ async def edit_spreadsheet(target_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
  sheet_id = sheet['properties']['sheetId']
405
 
406
+ # Use the copyTo method to copy the sheet to the target spreadsheet
407
+ copy_request = sheet_service.spreadsheets().sheets().copyTo(
408
  spreadsheetId=template_sheet_id,
409
+ sheetId=sheet_id,
410
+ body={"destinationSpreadsheetId": target_sheet_id}
411
+ )
412
+ copy_response = copy_request.execute()
413
+
414
+ logging.info(f"Sheet {sheet['properties']['title']} copied to {target_sheet_id} with new sheet ID {copy_response['sheetId']}.")
415
+
416
+ logging.info(f"All sheets from template {template_sheet_id} copied to {target_sheet_id}.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
417
  return {"message": "Spreadsheet copied successfully"}
418
 
419
  except Exception as e: