euler314 commited on
Commit
e8a1353
·
verified ·
1 Parent(s): 6bed1e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -0
app.py CHANGED
@@ -22,6 +22,61 @@ if CREDS_JSON:
22
  'https://www.googleapis.com/auth/drive.file']
23
  )
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  HTML_TEMPLATE = '''
26
  <!DOCTYPE html>
27
  <html>
 
22
  'https://www.googleapis.com/auth/drive.file']
23
  )
24
 
25
+ 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()
79
+
80
  HTML_TEMPLATE = '''
81
  <!DOCTYPE html>
82
  <html>