euler314 commited on
Commit
55b202d
·
verified ·
1 Parent(s): e8a1353

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -40
app.py CHANGED
@@ -26,53 +26,47 @@ def ensure_headers():
26
  try:
27
  sheets_service = build('sheets', 'v4', credentials=CREDENTIALS)
28
 
29
- # Get current values
30
- result = sheets_service.spreadsheets().values().get(
 
 
 
 
 
 
31
  spreadsheetId=SPREADSHEET_ID,
32
- range='Sheet1!A1:G1'
 
 
33
  ).execute()
34
 
35
- # If no values or empty first row, add headers
36
- if 'values' not in result or not result['values']:
37
- headers = [
38
- ['姓名 Name', '學號 Student ID', '購買商品 Product',
39
- '金額 Cost', '發票檔案 Receipt File', '時間 Timestamp', 'IP位址 IP Address']
40
- ]
41
-
42
- sheets_service.spreadsheets().values().update(
43
- spreadsheetId=SPREADSHEET_ID,
44
- range='Sheet1!A1:G1',
45
- valueInputOption='RAW',
46
- body={'values': headers}
47
- ).execute()
48
-
49
- # Format headers (make bold and center)
50
- requests = [{
51
- 'repeatCell': {
52
- 'range': {
53
- 'sheetId': 0,
54
- 'startRowIndex': 0,
55
- 'endRowIndex': 1
56
- },
57
- 'cell': {
58
- 'userEnteredFormat': {
59
- 'horizontalAlignment': 'CENTER',
60
- 'textFormat': {
61
- 'bold': True
62
- }
63
  }
64
- },
65
- 'fields': 'userEnteredFormat(horizontalAlignment,textFormat)'
66
- }
67
- }]
 
 
 
 
 
 
68
 
69
- sheets_service.spreadsheets().batchUpdate(
70
- spreadsheetId=SPREADSHEET_ID,
71
- body={'requests': requests}
72
- ).execute()
73
  except Exception as e:
74
  print(f"Error setting headers: {str(e)}")
75
-
76
  @app.before_first_request
77
  def initialize():
78
  ensure_headers()
 
26
  try:
27
  sheets_service = build('sheets', 'v4', credentials=CREDENTIALS)
28
 
29
+ # Directly update first row with headers
30
+ headers = [
31
+ ['姓名 Name', '學號 Student ID', '購買商品 Product',
32
+ '金額 Cost', '發票檔案 Receipt File', '時間 Timestamp', 'IP位址 IP Address']
33
+ ]
34
+
35
+ # Update A1:G1 with headers
36
+ sheets_service.spreadsheets().values().update(
37
  spreadsheetId=SPREADSHEET_ID,
38
+ range='Sheet1!A1:G1',
39
+ valueInputOption='RAW',
40
+ body={'values': headers}
41
  ).execute()
42
 
43
+ # Format headers to be bold and centered
44
+ requests = [{
45
+ 'repeatCell': {
46
+ 'range': {
47
+ 'sheetId': 0,
48
+ 'startRowIndex': 0,
49
+ 'endRowIndex': 1
50
+ },
51
+ 'cell': {
52
+ 'userEnteredFormat': {
53
+ 'horizontalAlignment': 'CENTER',
54
+ 'textFormat': {
55
+ 'bold': True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  }
57
+ }
58
+ },
59
+ 'fields': 'userEnteredFormat(horizontalAlignment,textFormat)'
60
+ }
61
+ }]
62
+
63
+ sheets_service.spreadsheets().batchUpdate(
64
+ spreadsheetId=SPREADSHEET_ID,
65
+ body={'requests': requests}
66
+ ).execute()
67
 
 
 
 
 
68
  except Exception as e:
69
  print(f"Error setting headers: {str(e)}")
 
70
  @app.before_first_request
71
  def initialize():
72
  ensure_headers()