Spaces:
Sleeping
Sleeping
ichieh commited on
Commit ·
4f4e7f8
1
Parent(s): 1714d7d
fix: 修正設定同步問題
Browse files1. 修改 Google Apps Script 的 doGet 函數,讓它返回設定資訊
2. 修改 Flask 應用程式的初始化代碼,正確讀取設定
- app.py +16 -14
- sync_settings.gs +23 -2
app.py
CHANGED
|
@@ -131,10 +131,10 @@ def init_db():
|
|
| 131 |
with app.app_context():
|
| 132 |
try:
|
| 133 |
# 檢查資料庫連接
|
| 134 |
-
db.
|
| 135 |
print("資料庫初始化成功")
|
| 136 |
-
|
| 137 |
-
#
|
| 138 |
if Participant.query.count() == 0:
|
| 139 |
print("資料庫為空,嘗試從 Google Sheets 恢復資料...")
|
| 140 |
try:
|
|
@@ -142,27 +142,29 @@ with app.app_context():
|
|
| 142 |
data = response.json()
|
| 143 |
|
| 144 |
if data.get('success'):
|
| 145 |
-
# 先恢復
|
| 146 |
-
|
| 147 |
-
total_tables =
|
| 148 |
-
seats_per_table =
|
|
|
|
| 149 |
print(f"從 Google Sheets 恢復設定:總桌數={total_tables}, 每桌人數={seats_per_table}")
|
| 150 |
|
| 151 |
-
# 清除現有的桌
|
| 152 |
Table.query.delete()
|
| 153 |
|
| 154 |
-
# 創建新的桌
|
| 155 |
-
for i in range(
|
| 156 |
-
table = Table(name=f
|
| 157 |
db.session.add(table)
|
|
|
|
| 158 |
db.session.commit()
|
| 159 |
print("桌次設定恢復完成")
|
| 160 |
|
| 161 |
# 然後恢復參與者資料
|
| 162 |
-
|
| 163 |
-
print(f"從 Google Sheets 找到 {len(
|
| 164 |
|
| 165 |
-
for participant_data in
|
| 166 |
try:
|
| 167 |
created_at = datetime.strptime(participant_data['created_at'], '%Y-%m-%d %H:%M:%S')
|
| 168 |
created_at = created_at - timedelta(hours=8) # 轉換回 UTC 時間
|
|
|
|
| 131 |
with app.app_context():
|
| 132 |
try:
|
| 133 |
# 檢查資料庫連接
|
| 134 |
+
db.session.execute('SELECT 1')
|
| 135 |
print("資料庫初始化成功")
|
| 136 |
+
|
| 137 |
+
# 如果資料庫是空的,從 Google Sheets 恢復資料
|
| 138 |
if Participant.query.count() == 0:
|
| 139 |
print("資料庫為空,嘗試從 Google Sheets 恢復資料...")
|
| 140 |
try:
|
|
|
|
| 142 |
data = response.json()
|
| 143 |
|
| 144 |
if data.get('success'):
|
| 145 |
+
# 首先恢復設定
|
| 146 |
+
settings = data.get('settings', {})
|
| 147 |
+
total_tables = settings.get('total_tables', 10)
|
| 148 |
+
seats_per_table = settings.get('seats_per_table', 10)
|
| 149 |
+
|
| 150 |
print(f"從 Google Sheets 恢復設定:總桌數={total_tables}, 每桌人數={seats_per_table}")
|
| 151 |
|
| 152 |
+
# 清除現有的桌子
|
| 153 |
Table.query.delete()
|
| 154 |
|
| 155 |
+
# 創建新的桌子
|
| 156 |
+
for i in range(total_tables):
|
| 157 |
+
table = Table(name=f'第 {i+1} 桌', capacity=seats_per_table)
|
| 158 |
db.session.add(table)
|
| 159 |
+
|
| 160 |
db.session.commit()
|
| 161 |
print("桌次設定恢復完成")
|
| 162 |
|
| 163 |
# 然後恢復參與者資料
|
| 164 |
+
participants = data.get('participants', [])
|
| 165 |
+
print(f"從 Google Sheets 找到 {len(participants)} 個參與者")
|
| 166 |
|
| 167 |
+
for participant_data in participants:
|
| 168 |
try:
|
| 169 |
created_at = datetime.strptime(participant_data['created_at'], '%Y-%m-%d %H:%M:%S')
|
| 170 |
created_at = created_at - timedelta(hours=8) # 轉換回 UTC 時間
|
sync_settings.gs
CHANGED
|
@@ -74,11 +74,31 @@ function doPost(e) {
|
|
| 74 |
function doGet() {
|
| 75 |
var sheet = SpreadsheetApp.getActiveSpreadsheet();
|
| 76 |
var participantsSheet = sheet.getSheetByName('Participants');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
if (!participantsSheet) {
|
| 79 |
return ContentService.createTextOutput(JSON.stringify({
|
| 80 |
-
success:
|
| 81 |
-
|
|
|
|
| 82 |
})).setMimeType(ContentService.MimeType.JSON);
|
| 83 |
}
|
| 84 |
|
|
@@ -122,6 +142,7 @@ function doGet() {
|
|
| 122 |
|
| 123 |
return ContentService.createTextOutput(JSON.stringify({
|
| 124 |
success: true,
|
|
|
|
| 125 |
participants: participants
|
| 126 |
})).setMimeType(ContentService.MimeType.JSON);
|
| 127 |
}
|
|
|
|
| 74 |
function doGet() {
|
| 75 |
var sheet = SpreadsheetApp.getActiveSpreadsheet();
|
| 76 |
var participantsSheet = sheet.getSheetByName('Participants');
|
| 77 |
+
var settingsSheet = sheet.getSheetByName('Settings');
|
| 78 |
+
|
| 79 |
+
// 獲取設定
|
| 80 |
+
var settings = {
|
| 81 |
+
total_tables: 10, // 預設值
|
| 82 |
+
seats_per_table: 10 // 預設值
|
| 83 |
+
};
|
| 84 |
+
|
| 85 |
+
if (settingsSheet) {
|
| 86 |
+
var settingsRange = settingsSheet.getDataRange();
|
| 87 |
+
var settingsValues = settingsRange.getValues();
|
| 88 |
+
|
| 89 |
+
// 從第二行開始尋找設定值
|
| 90 |
+
for (var i = 1; i < settingsValues.length; i++) {
|
| 91 |
+
var row = settingsValues[i];
|
| 92 |
+
if (row[0] === '總桌數') settings.total_tables = parseInt(row[1]) || 10;
|
| 93 |
+
if (row[0] === '每桌人數') settings.seats_per_table = parseInt(row[1]) || 10;
|
| 94 |
+
}
|
| 95 |
+
}
|
| 96 |
|
| 97 |
if (!participantsSheet) {
|
| 98 |
return ContentService.createTextOutput(JSON.stringify({
|
| 99 |
+
success: true,
|
| 100 |
+
settings: settings,
|
| 101 |
+
participants: []
|
| 102 |
})).setMimeType(ContentService.MimeType.JSON);
|
| 103 |
}
|
| 104 |
|
|
|
|
| 142 |
|
| 143 |
return ContentService.createTextOutput(JSON.stringify({
|
| 144 |
success: true,
|
| 145 |
+
settings: settings,
|
| 146 |
participants: participants
|
| 147 |
})).setMimeType(ContentService.MimeType.JSON);
|
| 148 |
}
|