Spaces:
Sleeping
Sleeping
File size: 10,134 Bytes
fde281b dc8d1ea fde281b 445d61d fde281b 445d61d fde281b dc8d1ea 749655f dc8d1ea 749655f dc8d1ea 749655f dc8d1ea e876179 fde281b e876179 3deafeb e876179 3deafeb e876179 fde281b 749655f fde281b dc8d1ea 749655f fde281b dd7cb5f 445d61d dd7cb5f 77adb92 e876179 445d61d e876179 445d61d a377724 dc8d1ea a377724 dc8d1ea a377724 dc8d1ea a377724 dc8d1ea dd7cb5f fde281b e876179 dc8d1ea fde281b 5fb5209 e876179 dc8d1ea 77adb92 e876179 77adb92 5fb5209 e876179 a377724 dc8d1ea a377724 dc8d1ea a377724 e876179 dc8d1ea 3deafeb dc8d1ea 445d61d dc8d1ea 445d61d e876179 dc8d1ea 71e734b dc8d1ea 445d61d dc8d1ea 445d61d dc8d1ea 445d61d dc8d1ea a1d202c dc8d1ea a1d202c 08794f7 dc8d1ea 08794f7 dc8d1ea dd7cb5f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | 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]]);
}
|