Spaces:
Sleeping
Sleeping
ichieh commited on
Commit ·
445d61d
1
Parent(s): 56698c2
桌次同步修正
Browse files- app.py +51 -47
- sync_settings.gs +43 -32
app.py
CHANGED
|
@@ -143,57 +143,61 @@ with app.app_context():
|
|
| 143 |
try:
|
| 144 |
response = requests.get(app.config['SETTINGS_SCRIPT_URL'])
|
| 145 |
print(f"Google Sheets 回應狀態碼: {response.status_code}")
|
|
|
|
|
|
|
| 146 |
data = response.json()
|
|
|
|
|
|
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
seats_per_table = int(settings.get('seats_per_table', 10))
|
| 158 |
-
print(f"從 Google Sheets 讀取設定:總桌數={total_tables}, 每桌人數={seats_per_table}")
|
| 159 |
-
|
| 160 |
-
# 創建桌子
|
| 161 |
-
for i in range(1, total_tables + 1):
|
| 162 |
-
table = Table(
|
| 163 |
-
name=f'第{i}桌',
|
| 164 |
-
capacity=seats_per_table
|
| 165 |
-
)
|
| 166 |
-
db.session.add(table)
|
| 167 |
-
db.session.commit()
|
| 168 |
-
print("桌次設定更新完成")
|
| 169 |
-
|
| 170 |
-
# 恢復參與者資料
|
| 171 |
-
participants_data = data.get('participants', [])
|
| 172 |
-
print(f"從 Google Sheets 讀取 {len(participants_data)} 位參與者資料")
|
| 173 |
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
except Exception as e:
|
| 198 |
print(f"同步資料時出錯: {str(e)}")
|
| 199 |
|
|
|
|
| 143 |
try:
|
| 144 |
response = requests.get(app.config['SETTINGS_SCRIPT_URL'])
|
| 145 |
print(f"Google Sheets 回應狀態碼: {response.status_code}")
|
| 146 |
+
print(f"Google Sheets 回應內容: {response.text}")
|
| 147 |
+
|
| 148 |
data = response.json()
|
| 149 |
+
if not data.get('success'):
|
| 150 |
+
raise Exception(f"同步失敗: {data.get('error', '未知錯誤')}")
|
| 151 |
|
| 152 |
+
# 清空現有資料
|
| 153 |
+
Participant.query.delete()
|
| 154 |
+
Table.query.delete()
|
| 155 |
+
db.session.commit()
|
| 156 |
+
|
| 157 |
+
# 從 Google Sheets 讀取設定
|
| 158 |
+
settings = data.get('settings', {})
|
| 159 |
+
if not settings:
|
| 160 |
+
raise Exception("無法從 Google Sheets 讀取設定")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
+
total_tables = int(settings.get('total_tables', 10))
|
| 163 |
+
seats_per_table = int(settings.get('seats_per_table', 10))
|
| 164 |
+
print(f"從 Google Sheets 讀取設定:總桌數={total_tables}, 每桌人數={seats_per_table}")
|
| 165 |
+
|
| 166 |
+
# 創建桌子
|
| 167 |
+
for i in range(1, total_tables + 1):
|
| 168 |
+
table = Table(
|
| 169 |
+
name=f'第{i}桌',
|
| 170 |
+
capacity=seats_per_table
|
| 171 |
+
)
|
| 172 |
+
db.session.add(table)
|
| 173 |
+
db.session.commit()
|
| 174 |
+
print("桌次設定更新完成")
|
| 175 |
+
|
| 176 |
+
# 恢復參與者資料
|
| 177 |
+
participants_data = data.get('participants', [])
|
| 178 |
+
print(f"從 Google Sheets 讀取 {len(participants_data)} 位參與者資料")
|
| 179 |
+
|
| 180 |
+
for participant_data in participants_data:
|
| 181 |
+
created_at_str = participant_data['created_at'].replace('T', ' ').replace('.000Z', '')
|
| 182 |
+
created_at = datetime.strptime(created_at_str, '%Y-%m-%d %H:%M:%S')
|
| 183 |
|
| 184 |
+
participant = Participant(
|
| 185 |
+
nickname=participant_data['nickname'],
|
| 186 |
+
email=participant_data['email'],
|
| 187 |
+
fb_link=participant_data.get('fb_link', ''),
|
| 188 |
+
introduction=participant_data.get('introduction', ''),
|
| 189 |
+
table_id=participant_data['table_id'],
|
| 190 |
+
seat_number=participant_data['seat_number'],
|
| 191 |
+
is_table_leader=participant_data.get('is_table_leader', False),
|
| 192 |
+
created_at=created_at,
|
| 193 |
+
has_paid=participant_data.get('has_paid', False),
|
| 194 |
+
note=participant_data.get('note', '')
|
| 195 |
+
)
|
| 196 |
+
db.session.add(participant)
|
| 197 |
+
print(f"已更新參與者: {participant.nickname}")
|
| 198 |
+
|
| 199 |
+
db.session.commit()
|
| 200 |
+
print("參與者資料更新完成")
|
| 201 |
except Exception as e:
|
| 202 |
print(f"同步資料時出錯: {str(e)}")
|
| 203 |
|
sync_settings.gs
CHANGED
|
@@ -17,22 +17,12 @@ function doGet() {
|
|
| 17 |
for (var i = 1; i < settingsValues.length; i++) {
|
| 18 |
var row = settingsValues[i];
|
| 19 |
if (row[0] === '總桌數') {
|
| 20 |
-
|
| 21 |
-
settings.total_tables = isNaN(value) ? 10 : value;
|
| 22 |
}
|
| 23 |
if (row[0] === '每桌人數') {
|
| 24 |
-
|
| 25 |
-
settings.seats_per_table = isNaN(value) ? 10 : value;
|
| 26 |
}
|
| 27 |
}
|
| 28 |
-
} else {
|
| 29 |
-
// 如果沒有設定表,創建一個
|
| 30 |
-
settingsSheet = sheet.insertSheet('Settings');
|
| 31 |
-
settingsSheet.getRange('A1:B1').setValues([['設定項目', '值']]);
|
| 32 |
-
settingsSheet.getRange('A2:B3').setValues([
|
| 33 |
-
['總桌數', settings.total_tables],
|
| 34 |
-
['每桌人數', settings.seats_per_table]
|
| 35 |
-
]);
|
| 36 |
}
|
| 37 |
|
| 38 |
// 獲取參與者資料
|
|
@@ -70,20 +60,27 @@ function doGet() {
|
|
| 70 |
}
|
| 71 |
|
| 72 |
function doPost(e) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
var sheet = SpreadsheetApp.getActiveSpreadsheet();
|
| 74 |
-
var data = JSON.parse(e.postData.contents);
|
| 75 |
-
var action = data.action;
|
| 76 |
-
|
| 77 |
-
Logger.log("Received action: " + action);
|
| 78 |
-
Logger.log("Received data: " + JSON.stringify(data));
|
| 79 |
|
| 80 |
try {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
if (action === 'backupParticipants') {
|
| 82 |
return backupParticipants(sheet, data.participants);
|
| 83 |
-
} else if (action === 'update_tables') {
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
return updateSettings(sheet, data.settings);
|
| 87 |
}
|
| 88 |
|
| 89 |
return ContentService.createTextOutput(JSON.stringify({
|
|
@@ -101,6 +98,13 @@ function doPost(e) {
|
|
| 101 |
}
|
| 102 |
|
| 103 |
function backupParticipants(sheet, participants) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
var participantsSheet = sheet.getSheetByName('Participants') || sheet.insertSheet('Participants');
|
| 105 |
participantsSheet.clear();
|
| 106 |
|
|
@@ -109,19 +113,19 @@ function backupParticipants(sheet, participants) {
|
|
| 109 |
'ID', '暱稱', 'Email', 'FB連結', '自我介紹', '桌次', '座位號', '是否為桌長', '報名時間', '付款狀態', '備註'
|
| 110 |
]]);
|
| 111 |
|
| 112 |
-
if (participants
|
| 113 |
var values = participants.map(function(p) {
|
| 114 |
return [
|
| 115 |
-
p.id,
|
| 116 |
-
p.nickname,
|
| 117 |
-
p.email,
|
| 118 |
p.fb_link || '',
|
| 119 |
p.introduction || '',
|
| 120 |
-
p.table_id,
|
| 121 |
-
p.seat_number,
|
| 122 |
-
p.is_table_leader,
|
| 123 |
-
p.created_at,
|
| 124 |
-
p.has_paid,
|
| 125 |
p.note || ''
|
| 126 |
];
|
| 127 |
});
|
|
@@ -136,6 +140,13 @@ function backupParticipants(sheet, participants) {
|
|
| 136 |
}
|
| 137 |
|
| 138 |
function updateSettings(sheet, settings) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
var settingsSheet = sheet.getSheetByName('Settings') || sheet.insertSheet('Settings');
|
| 140 |
settingsSheet.clear();
|
| 141 |
|
|
@@ -143,8 +154,8 @@ function updateSettings(sheet, settings) {
|
|
| 143 |
settingsSheet.getRange('A1:B1').setValues([['設定項目', '值']]);
|
| 144 |
|
| 145 |
// 更新設定值
|
| 146 |
-
var total_tables = settings.total_tables || 10;
|
| 147 |
-
var seats_per_table = settings.seats_per_table || 10;
|
| 148 |
|
| 149 |
settingsSheet.getRange('A2:B3').setValues([
|
| 150 |
['總桌數', total_tables],
|
|
|
|
| 17 |
for (var i = 1; i < settingsValues.length; i++) {
|
| 18 |
var row = settingsValues[i];
|
| 19 |
if (row[0] === '總桌數') {
|
| 20 |
+
settings.total_tables = parseInt(row[1]) || 10;
|
|
|
|
| 21 |
}
|
| 22 |
if (row[0] === '每桌人數') {
|
| 23 |
+
settings.seats_per_table = parseInt(row[1]) || 10;
|
|
|
|
| 24 |
}
|
| 25 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
}
|
| 27 |
|
| 28 |
// 獲取參與者資料
|
|
|
|
| 60 |
}
|
| 61 |
|
| 62 |
function doPost(e) {
|
| 63 |
+
if (!e || !e.postData || !e.postData.contents) {
|
| 64 |
+
return ContentService.createTextOutput(JSON.stringify({
|
| 65 |
+
success: false,
|
| 66 |
+
error: 'Invalid request format'
|
| 67 |
+
})).setMimeType(ContentService.MimeType.JSON);
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
var sheet = SpreadsheetApp.getActiveSpreadsheet();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
try {
|
| 73 |
+
var data = JSON.parse(e.postData.contents);
|
| 74 |
+
var action = data.action;
|
| 75 |
+
|
| 76 |
+
Logger.log("Received action: " + action);
|
| 77 |
+
Logger.log("Received data: " + JSON.stringify(data));
|
| 78 |
+
|
| 79 |
if (action === 'backupParticipants') {
|
| 80 |
return backupParticipants(sheet, data.participants);
|
| 81 |
+
} else if (action === 'update_tables' || action === 'updateSettings') {
|
| 82 |
+
var settings = action === 'update_tables' ? data : data.settings;
|
| 83 |
+
return updateSettings(sheet, settings);
|
|
|
|
| 84 |
}
|
| 85 |
|
| 86 |
return ContentService.createTextOutput(JSON.stringify({
|
|
|
|
| 98 |
}
|
| 99 |
|
| 100 |
function backupParticipants(sheet, participants) {
|
| 101 |
+
if (!participants) {
|
| 102 |
+
return ContentService.createTextOutput(JSON.stringify({
|
| 103 |
+
success: false,
|
| 104 |
+
error: 'No participants data provided'
|
| 105 |
+
})).setMimeType(ContentService.MimeType.JSON);
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
var participantsSheet = sheet.getSheetByName('Participants') || sheet.insertSheet('Participants');
|
| 109 |
participantsSheet.clear();
|
| 110 |
|
|
|
|
| 113 |
'ID', '暱稱', 'Email', 'FB連結', '自我介紹', '桌次', '座位號', '是否為桌長', '報名時間', '付款狀態', '備註'
|
| 114 |
]]);
|
| 115 |
|
| 116 |
+
if (participants.length > 0) {
|
| 117 |
var values = participants.map(function(p) {
|
| 118 |
return [
|
| 119 |
+
p.id || '',
|
| 120 |
+
p.nickname || '',
|
| 121 |
+
p.email || '',
|
| 122 |
p.fb_link || '',
|
| 123 |
p.introduction || '',
|
| 124 |
+
p.table_id || 0,
|
| 125 |
+
p.seat_number || 0,
|
| 126 |
+
Boolean(p.is_table_leader),
|
| 127 |
+
p.created_at || '',
|
| 128 |
+
Boolean(p.has_paid),
|
| 129 |
p.note || ''
|
| 130 |
];
|
| 131 |
});
|
|
|
|
| 140 |
}
|
| 141 |
|
| 142 |
function updateSettings(sheet, settings) {
|
| 143 |
+
if (!settings || (typeof settings.total_tables === 'undefined' && typeof settings.seats_per_table === 'undefined')) {
|
| 144 |
+
return ContentService.createTextOutput(JSON.stringify({
|
| 145 |
+
success: false,
|
| 146 |
+
error: 'Invalid settings data'
|
| 147 |
+
})).setMimeType(ContentService.MimeType.JSON);
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
var settingsSheet = sheet.getSheetByName('Settings') || sheet.insertSheet('Settings');
|
| 151 |
settingsSheet.clear();
|
| 152 |
|
|
|
|
| 154 |
settingsSheet.getRange('A1:B1').setValues([['設定項目', '值']]);
|
| 155 |
|
| 156 |
// 更新設定值
|
| 157 |
+
var total_tables = parseInt(settings.total_tables) || 10;
|
| 158 |
+
var seats_per_table = parseInt(settings.seats_per_table) || 10;
|
| 159 |
|
| 160 |
settingsSheet.getRange('A2:B3').setValues([
|
| 161 |
['總桌數', total_tables],
|