Spaces:
Sleeping
Sleeping
Download sync_settings.gs from fromnowoncoding/eventinvite: direct link, hf CLI and curl.
- Browser
- Download file 10.1 kB
-
https://huggingface.co/spaces/fromnowoncoding/eventinvite/resolve/main/sync_settings.gs
- Command line
-
hf download hf://spaces/fromnowoncoding/eventinvite/sync_settings.gs
-
curl -L -o sync_settings.gs https://huggingface.co/spaces/fromnowoncoding/eventinvite/resolve/main/sync_settings.gs
10.1 kB
| function doGet() { | |
| var sheet = SpreadsheetApp.getActiveSpreadsheet(); | |
| var participantsSheet = sheet.getSheetByName('Participants'); | |
| var settingsSheet = sheet.getSheetByName('Settings'); | |
| var tablesSheet = sheet.getSheetByName('Tables'); // 新增桌次表格 | |
| // 獲取設定 | |
| var settings = { | |
| total_tables: 10, // 預設值 | |
| seats_per_table: 10 // 預設值 | |
| }; | |
| if (settingsSheet) { | |
| var settingsRange = settingsSheet.getDataRange(); | |
| var settingsValues = settingsRange.getValues(); | |
| // 從第二行開始尋找設定值 | |
| for (var i = 1; i < settingsValues.length; i++) { | |
| var row = settingsValues[i]; | |
| if (row[0] === '總桌數') { | |
| settings.total_tables = parseInt(row[1]) || 10; | |
| } | |
| if (row[0] === '每桌人數') { | |
| settings.seats_per_table = parseInt(row[1]) || 10; | |
| } | |
| } | |
| } | |
| // 獲取桌次資料 | |
| var tables = []; | |
| if (tablesSheet) { | |
| var tablesData = tablesSheet.getDataRange().getValues(); | |
| Logger.log("Tables sheet data: " + JSON.stringify(tablesData)); | |
| if (tablesData.length > 0) { | |
| var headers = tablesData[0]; | |
| var idIndex = headers.indexOf('Table ID'); | |
| var nameIndex = headers.indexOf('Table Name'); | |
| Logger.log("Table headers found - ID index: " + idIndex + ", Name index: " + nameIndex); | |
| if (idIndex !== -1 && nameIndex !== -1) { | |
| for (var i = 1; i < tablesData.length; i++) { | |
| var row = tablesData[i]; | |
| var tableId = row[idIndex]; | |
| var tableName = row[nameIndex]; | |
| if (tableId) { // 只處理有 ID 的行 | |
| tableId = parseInt(tableId); | |
| tableName = tableName || ''; | |
| Logger.log("Found table: ID=" + tableId + ", Name=" + tableName); | |
| tables.push({ | |
| id: tableId, | |
| name: tableName | |
| }); | |
| } | |
| } | |
| } else { | |
| Logger.log("Error: Table ID or Table Name column not found"); | |
| } | |
| } | |
| } | |
| Logger.log("Final tables data: " + JSON.stringify(tables)); | |
| // 獲取參與者資料 | |
| var participants = []; | |
| if (participantsSheet) { | |
| var data = participantsSheet.getDataRange().getValues(); | |
| var headers = data[0]; | |
| for (var i = 1; i < data.length; i++) { | |
| var row = data[i]; | |
| if (row[0]) { // 只處理有 ID 的行 | |
| var participant = { | |
| id: row[0], | |
| nickname: row[1], | |
| email: row[2], | |
| fb_link: row[3] || '', | |
| introduction: row[4] || '', | |
| table_id: parseInt(row[5]) || 0, | |
| seat_number: parseInt(row[6]) || 0, | |
| is_table_leader: row[7] === true || row[7] === 'TRUE', // 使用 TRUE/FALSE | |
| created_at: row[8], | |
| has_paid: row[9] === true || row[9] === 'TRUE', // 使用 TRUE/FALSE | |
| note: row[10] || '' | |
| }; | |
| participants.push(participant); | |
| } | |
| } | |
| } | |
| var response = { | |
| success: true, | |
| participants: participants, | |
| settings: settings, | |
| tables: tables | |
| }; | |
| Logger.log("Final response: " + JSON.stringify(response)); | |
| return ContentService.createTextOutput(JSON.stringify(response)) | |
| .setMimeType(ContentService.MimeType.JSON); | |
| } | |
| function doPost(e) { | |
| if (!e || !e.postData || !e.postData.contents) { | |
| return ContentService.createTextOutput(JSON.stringify({ | |
| success: false, | |
| error: 'Invalid request format' | |
| })).setMimeType(ContentService.MimeType.JSON); | |
| } | |
| var sheet = SpreadsheetApp.getActiveSpreadsheet(); | |
| try { | |
| var data = JSON.parse(e.postData.contents); | |
| var action = data.action; | |
| Logger.log("Received action: " + action); | |
| Logger.log("Received data: " + JSON.stringify(data)); | |
| if (action === 'backupParticipants') { | |
| return backupParticipants(sheet, data.participants); | |
| } else if (action === 'update_tables' || action === 'updateSettings') { | |
| var settings = action === 'update_tables' ? data : data.settings; | |
| return updateSettings(sheet, settings); | |
| } else if (action === 'update_table_name') { | |
| var tableId = data.table_id; | |
| var name = data.name; | |
| // 更新桌次名稱 | |
| var tablesSheet = sheet.getSheetByName('Tables'); | |
| if (!tablesSheet) { | |
| tablesSheet = sheet.insertSheet('Tables'); | |
| tablesSheet.getRange(1, 1, 1, 2).setValues([['ID', '名稱']]); | |
| } | |
| // 尋找或創建桌次行 | |
| var tableRow = findTableRow(tableId, tablesSheet); | |
| if (tableRow === 0) { | |
| // 如果找不到桌次,在最後一行添加 | |
| tableRow = tablesSheet.getLastRow() + 1; | |
| tablesSheet.getRange(tableRow, 1, 1, 2).setValues([[tableId, name]]); | |
| } else { | |
| // 更新現有桌次名稱 | |
| tablesSheet.getRange(tableRow, 2).setValue(name); | |
| } | |
| return ContentService.createTextOutput(JSON.stringify({ | |
| success: true, | |
| message: '桌次名稱已更新' | |
| })).setMimeType(ContentService.MimeType.JSON); | |
| } | |
| return ContentService.createTextOutput(JSON.stringify({ | |
| success: false, | |
| error: 'Unknown action' | |
| })).setMimeType(ContentService.MimeType.JSON); | |
| } catch (error) { | |
| Logger.log("Error: " + error.toString()); | |
| return ContentService.createTextOutput(JSON.stringify({ | |
| success: false, | |
| error: error.toString() | |
| })).setMimeType(ContentService.MimeType.JSON); | |
| } | |
| } | |
| // 尋找桌次在表格中的行數 | |
| function findTableRow(tableId, sheet) { | |
| var data = sheet.getDataRange().getValues(); | |
| for (var i = 1; i < data.length; i++) { | |
| if (data[i][0] == tableId) { | |
| return i + 1; // 加 1 是因為 getValues() 從 0 開始,但 sheet 從 1 開始 | |
| } | |
| } | |
| return 0; // 沒找到 | |
| } | |
| function backupParticipants(sheet, participants) { | |
| try { | |
| var participantsSheet = sheet.getSheetByName('Participants'); | |
| if (!participantsSheet) { | |
| participantsSheet = sheet.insertSheet('Participants'); | |
| } | |
| // 設置表頭 | |
| var headers = [ | |
| 'ID', '暱稱', 'Email', 'FB連結', '自我介紹', | |
| '桌次', '座位號', '是否桌長', '報名時間', '是否付款', '備註' | |
| ]; | |
| participantsSheet.getRange(1, 1, 1, headers.length).setValues([headers]); | |
| // 清空舊資料(保留表頭) | |
| var lastRow = participantsSheet.getLastRow(); | |
| if (lastRow > 1) { | |
| participantsSheet.getRange(2, 1, lastRow - 1, headers.length).clear(); | |
| } | |
| // 寫入新資料 | |
| if (participants && participants.length > 0) { | |
| var values = participants.map(function(p) { | |
| return [ | |
| p.id, | |
| p.nickname, | |
| p.email, | |
| p.fb_link || '', | |
| p.introduction || '', | |
| p.table_id || 0, | |
| p.seat_number || 0, | |
| p.is_table_leader ? 'TRUE' : 'FALSE', // 使用 TRUE/FALSE | |
| p.created_at || '', | |
| p.has_paid ? 'TRUE' : 'FALSE', // 使用 TRUE/FALSE | |
| p.note || '' | |
| ]; | |
| }); | |
| participantsSheet.getRange(2, 1, values.length, headers.length).setValues(values); | |
| } | |
| return ContentService.createTextOutput(JSON.stringify({ | |
| success: true, | |
| message: '參與者資料已更新' | |
| })).setMimeType(ContentService.MimeType.JSON); | |
| } catch (error) { | |
| Logger.log("Error in backupParticipants: " + error.toString()); | |
| return ContentService.createTextOutput(JSON.stringify({ | |
| success: false, | |
| error: error.toString() | |
| })).setMimeType(ContentService.MimeType.JSON); | |
| } | |
| } | |
| function updateSettings(sheet, settings) { | |
| try { | |
| var settingsSheet = sheet.getSheetByName('Settings'); | |
| if (!settingsSheet) { | |
| settingsSheet = sheet.insertSheet('Settings'); | |
| settingsSheet.getRange(1, 1, 1, 2).setValues([['設定項目', '值']]); | |
| } | |
| // 更新總桌數 | |
| if (settings.totalTables || settings.total_tables) { | |
| var totalTables = settings.totalTables || settings.total_tables; | |
| updateSettingValue(settingsSheet, '總桌數', totalTables); | |
| // 同時更新 Tables 工作表 | |
| var tablesSheet = sheet.getSheetByName('Tables'); | |
| if (!tablesSheet) { | |
| tablesSheet = sheet.insertSheet('Tables'); | |
| tablesSheet.getRange(1, 1, 1, 2).setValues([['ID', '名稱']]); | |
| } | |
| // 獲取當前所有桌次資料 | |
| var currentData = tablesSheet.getDataRange().getValues(); | |
| var currentTables = {}; | |
| for (var i = 1; i < currentData.length; i++) { | |
| if (currentData[i][0]) { | |
| currentTables[currentData[i][0]] = currentData[i][1] || ''; | |
| } | |
| } | |
| // 準備新的桌次資料 | |
| var newData = [['ID', '名稱']]; // 標題行 | |
| for (var i = 1; i <= totalTables; i++) { | |
| newData.push([i, currentTables[i] || '']); // 保留現有的桌次名稱 | |
| } | |
| // 清除現有資料並寫入新資料 | |
| tablesSheet.clear(); | |
| tablesSheet.getRange(1, 1, newData.length, 2).setValues(newData); | |
| } | |
| // 更新每桌人數 | |
| if (settings.seatsPerTable || settings.seats_per_table) { | |
| var seatsPerTable = settings.seatsPerTable || settings.seats_per_table; | |
| updateSettingValue(settingsSheet, '每桌人數', seatsPerTable); | |
| } | |
| return ContentService.createTextOutput(JSON.stringify({ | |
| success: true, | |
| message: '設定已更新' | |
| })).setMimeType(ContentService.MimeType.JSON); | |
| } catch (error) { | |
| Logger.log("Error in updateSettings: " + error.toString()); | |
| return ContentService.createTextOutput(JSON.stringify({ | |
| success: false, | |
| error: error.toString() | |
| })).setMimeType(ContentService.MimeType.JSON); | |
| } | |
| } | |
| function updateSettingValue(sheet, key, value) { | |
| var data = sheet.getDataRange().getValues(); | |
| var rowIndex = -1; | |
| // 尋找設定項目 | |
| for (var i = 1; i < data.length; i++) { | |
| if (data[i][0] === key) { | |
| rowIndex = i + 1; | |
| break; | |
| } | |
| } | |
| // 如果找不到,就新增一行 | |
| if (rowIndex === -1) { | |
| rowIndex = sheet.getLastRow() + 1; | |
| } | |
| // 更新值 | |
| sheet.getRange(rowIndex, 1, 1, 2).setValues([[key, value]]); | |
| } | |