Spaces:
Sleeping
Sleeping
ichieh commited on
Commit ·
749655f
1
Parent(s): f3205c7
修改桌次名稱同步
Browse files- app.py +11 -12
- sync_settings.gs +35 -11
app.py
CHANGED
|
@@ -131,32 +131,31 @@ def init_db():
|
|
| 131 |
print(f"- 每桌人數: {seats_per_table}")
|
| 132 |
print(f"- 桌次資料: {tables_data}")
|
| 133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
# 檢查現有桌次
|
| 135 |
existing_tables = {table.id: table for table in Table.query.all()}
|
| 136 |
|
| 137 |
# 創建或更新桌次
|
| 138 |
for i in range(1, total_tables + 1):
|
| 139 |
-
# 尋找對應的桌次名稱
|
| 140 |
-
table_name = None
|
| 141 |
-
for table_data in tables_data:
|
| 142 |
-
if table_data.get('id') == i:
|
| 143 |
-
table_name = table_data.get('name')
|
| 144 |
-
break
|
| 145 |
-
|
| 146 |
if i in existing_tables:
|
| 147 |
table = existing_tables[i]
|
| 148 |
table.capacity = seats_per_table
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
|
|
|
| 152 |
else:
|
|
|
|
| 153 |
table = Table(
|
| 154 |
id=i,
|
| 155 |
-
name=
|
| 156 |
capacity=seats_per_table
|
| 157 |
)
|
| 158 |
db.session.add(table)
|
| 159 |
-
print(f"創建第 {i} 桌,名稱為: {
|
| 160 |
|
| 161 |
# 刪除多餘的桌次
|
| 162 |
for table_id, table in existing_tables.items():
|
|
|
|
| 131 |
print(f"- 每桌人數: {seats_per_table}")
|
| 132 |
print(f"- 桌次資料: {tables_data}")
|
| 133 |
|
| 134 |
+
# 建立桌次 ID 到名稱的對應
|
| 135 |
+
table_names = {table['id']: table['name'] for table in tables_data}
|
| 136 |
+
print(f"桌次名稱對應: {table_names}")
|
| 137 |
+
|
| 138 |
# 檢查現有桌次
|
| 139 |
existing_tables = {table.id: table for table in Table.query.all()}
|
| 140 |
|
| 141 |
# 創建或更新桌次
|
| 142 |
for i in range(1, total_tables + 1):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
if i in existing_tables:
|
| 144 |
table = existing_tables[i]
|
| 145 |
table.capacity = seats_per_table
|
| 146 |
+
# 更新名稱(如果在 Google Sheets 中有設定)
|
| 147 |
+
if i in table_names:
|
| 148 |
+
table.name = table_names[i]
|
| 149 |
+
print(f"更新第 {i} 桌名稱為: {table_names[i]}")
|
| 150 |
else:
|
| 151 |
+
# 創建新桌次,使用 Google Sheets 中的名稱(如果有)
|
| 152 |
table = Table(
|
| 153 |
id=i,
|
| 154 |
+
name=table_names.get(i, ''),
|
| 155 |
capacity=seats_per_table
|
| 156 |
)
|
| 157 |
db.session.add(table)
|
| 158 |
+
print(f"創建第 {i} 桌,名稱為: {table_names.get(i, '(空)')}")
|
| 159 |
|
| 160 |
# 刪除多餘的桌次
|
| 161 |
for table_id, table in existing_tables.items():
|
sync_settings.gs
CHANGED
|
@@ -30,19 +30,39 @@ function doGet() {
|
|
| 30 |
var tables = [];
|
| 31 |
if (tablesSheet) {
|
| 32 |
var tablesData = tablesSheet.getDataRange().getValues();
|
| 33 |
-
|
| 34 |
|
| 35 |
-
|
| 36 |
-
var
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
}
|
| 44 |
}
|
| 45 |
}
|
|
|
|
| 46 |
|
| 47 |
// 獲取參與者資料
|
| 48 |
var participants = [];
|
|
@@ -71,12 +91,16 @@ function doGet() {
|
|
| 71 |
}
|
| 72 |
}
|
| 73 |
|
| 74 |
-
|
| 75 |
success: true,
|
| 76 |
participants: participants,
|
| 77 |
settings: settings,
|
| 78 |
tables: tables
|
| 79 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
}
|
| 81 |
|
| 82 |
function doPost(e) {
|
|
|
|
| 30 |
var tables = [];
|
| 31 |
if (tablesSheet) {
|
| 32 |
var tablesData = tablesSheet.getDataRange().getValues();
|
| 33 |
+
Logger.log("Tables sheet data: " + JSON.stringify(tablesData));
|
| 34 |
|
| 35 |
+
if (tablesData.length > 0) {
|
| 36 |
+
var headers = tablesData[0];
|
| 37 |
+
var idIndex = headers.indexOf('Table ID');
|
| 38 |
+
var nameIndex = headers.indexOf('Table Name');
|
| 39 |
+
|
| 40 |
+
Logger.log("Table headers found - ID index: " + idIndex + ", Name index: " + nameIndex);
|
| 41 |
+
|
| 42 |
+
if (idIndex !== -1 && nameIndex !== -1) {
|
| 43 |
+
for (var i = 1; i < tablesData.length; i++) {
|
| 44 |
+
var row = tablesData[i];
|
| 45 |
+
var tableId = row[idIndex];
|
| 46 |
+
var tableName = row[nameIndex];
|
| 47 |
+
|
| 48 |
+
if (tableId) { // 只處理有 ID 的行
|
| 49 |
+
tableId = parseInt(tableId);
|
| 50 |
+
tableName = tableName || '';
|
| 51 |
+
|
| 52 |
+
Logger.log("Found table: ID=" + tableId + ", Name=" + tableName);
|
| 53 |
+
|
| 54 |
+
tables.push({
|
| 55 |
+
id: tableId,
|
| 56 |
+
name: tableName
|
| 57 |
+
});
|
| 58 |
+
}
|
| 59 |
+
}
|
| 60 |
+
} else {
|
| 61 |
+
Logger.log("Error: Table ID or Table Name column not found");
|
| 62 |
}
|
| 63 |
}
|
| 64 |
}
|
| 65 |
+
Logger.log("Final tables data: " + JSON.stringify(tables));
|
| 66 |
|
| 67 |
// 獲取參與者資料
|
| 68 |
var participants = [];
|
|
|
|
| 91 |
}
|
| 92 |
}
|
| 93 |
|
| 94 |
+
var response = {
|
| 95 |
success: true,
|
| 96 |
participants: participants,
|
| 97 |
settings: settings,
|
| 98 |
tables: tables
|
| 99 |
+
};
|
| 100 |
+
Logger.log("Final response: " + JSON.stringify(response));
|
| 101 |
+
|
| 102 |
+
return ContentService.createTextOutput(JSON.stringify(response))
|
| 103 |
+
.setMimeType(ContentService.MimeType.JSON);
|
| 104 |
}
|
| 105 |
|
| 106 |
function doPost(e) {
|