Spaces:
Sleeping
Sleeping
ichieh commited on
Commit ·
3ee554d
1
Parent(s): d45a62b
Add export API and Google Apps Script for syncing to spreadsheet
Browse files- app.py +23 -0
- sync_to_sheets.gs +75 -0
app.py
CHANGED
|
@@ -259,5 +259,28 @@ def register():
|
|
| 259 |
print(f"Error in registration: {e}")
|
| 260 |
return jsonify({'error': str(e)}), 400
|
| 261 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
if __name__ == '__main__':
|
| 263 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 259 |
print(f"Error in registration: {e}")
|
| 260 |
return jsonify({'error': str(e)}), 400
|
| 261 |
|
| 262 |
+
@app.route('/api/participants/export', methods=['GET'])
|
| 263 |
+
def export_participants():
|
| 264 |
+
try:
|
| 265 |
+
# 獲取所有桌子的資料
|
| 266 |
+
tables = Table.query.all()
|
| 267 |
+
export_data = []
|
| 268 |
+
|
| 269 |
+
for table in tables:
|
| 270 |
+
participants = Participant.query.filter_by(table_id=table.id).all()
|
| 271 |
+
for participant in participants:
|
| 272 |
+
export_data.append({
|
| 273 |
+
'table_name': table.name,
|
| 274 |
+
'table_capacity': table.capacity,
|
| 275 |
+
'nickname': participant.nickname,
|
| 276 |
+
'email': participant.email,
|
| 277 |
+
'fb_link': participant.fb_link,
|
| 278 |
+
'registration_time': participant.created_at.strftime('%Y-%m-%d %H:%M:%S')
|
| 279 |
+
})
|
| 280 |
+
|
| 281 |
+
return jsonify(export_data)
|
| 282 |
+
except Exception as e:
|
| 283 |
+
return jsonify({'error': str(e)}), 500
|
| 284 |
+
|
| 285 |
if __name__ == '__main__':
|
| 286 |
app.run(host='0.0.0.0', port=7860)
|
sync_to_sheets.gs
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// 設置您的應用程序 URL
|
| 2 |
+
const APP_URL = 'YOUR_APP_URL/api/participants/export'; // 請替換為您的 Hugging Face Space URL
|
| 3 |
+
|
| 4 |
+
// 設置工作表名稱
|
| 5 |
+
const SHEET_NAME = '報名者資料';
|
| 6 |
+
|
| 7 |
+
// 設置表頭
|
| 8 |
+
const HEADERS = [
|
| 9 |
+
'桌號',
|
| 10 |
+
'座位容量',
|
| 11 |
+
'暱稱',
|
| 12 |
+
'Email',
|
| 13 |
+
'FB連結',
|
| 14 |
+
'報名時間'
|
| 15 |
+
];
|
| 16 |
+
|
| 17 |
+
function updateSpreadsheet() {
|
| 18 |
+
try {
|
| 19 |
+
// 獲取或創建工作表
|
| 20 |
+
const ss = SpreadsheetApp.getActiveSpreadsheet();
|
| 21 |
+
let sheet = ss.getSheetByName(SHEET_NAME);
|
| 22 |
+
if (!sheet) {
|
| 23 |
+
sheet = ss.insertSheet(SHEET_NAME);
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
// 清除現有資料
|
| 27 |
+
sheet.clear();
|
| 28 |
+
|
| 29 |
+
// 設置表頭
|
| 30 |
+
sheet.getRange(1, 1, 1, HEADERS.length).setValues([HEADERS]);
|
| 31 |
+
|
| 32 |
+
// 從應用程序獲取資料
|
| 33 |
+
const response = UrlFetchApp.fetch(APP_URL);
|
| 34 |
+
const participants = JSON.parse(response.getContentText());
|
| 35 |
+
|
| 36 |
+
if (participants.length > 0) {
|
| 37 |
+
// 準備資料
|
| 38 |
+
const data = participants.map(p => [
|
| 39 |
+
p.table_name,
|
| 40 |
+
p.table_capacity,
|
| 41 |
+
p.nickname,
|
| 42 |
+
p.email,
|
| 43 |
+
p.fb_link,
|
| 44 |
+
p.registration_time
|
| 45 |
+
]);
|
| 46 |
+
|
| 47 |
+
// 寫入資料
|
| 48 |
+
sheet.getRange(2, 1, data.length, HEADERS.length).setValues(data);
|
| 49 |
+
|
| 50 |
+
// 自動調整欄寬
|
| 51 |
+
sheet.autoResizeColumns(1, HEADERS.length);
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
// 設置最後更新時間
|
| 55 |
+
sheet.getRange(1, HEADERS.length + 2).setValue('最後更新時間:');
|
| 56 |
+
sheet.getRange(1, HEADERS.length + 3).setValue(new Date().toLocaleString());
|
| 57 |
+
|
| 58 |
+
Logger.log('Spreadsheet updated successfully');
|
| 59 |
+
} catch (error) {
|
| 60 |
+
Logger.log('Error updating spreadsheet: ' + error.toString());
|
| 61 |
+
}
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
// 設置觸發器,每5分鐘執行一次
|
| 65 |
+
function createTrigger() {
|
| 66 |
+
// 刪除現有的觸發器
|
| 67 |
+
const triggers = ScriptApp.getProjectTriggers();
|
| 68 |
+
triggers.forEach(trigger => ScriptApp.deleteTrigger(trigger));
|
| 69 |
+
|
| 70 |
+
// 創建新的觸發器
|
| 71 |
+
ScriptApp.newTrigger('updateSpreadsheet')
|
| 72 |
+
.timeBased()
|
| 73 |
+
.everyMinutes(5)
|
| 74 |
+
.create();
|
| 75 |
+
}
|