Skip to content

Commit 36ffb03

Browse files
authored
Fix CSV header parsing by trimming header names
Trim header names when parsing CSV to ensure accurate matching.
1 parent 33bd4e5 commit 36ffb03

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

index.html

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,17 @@ <h3 class="text-lg font-semibold text-gray-700 mb-4">異味發生熱點時段 (2
190190
Papa.parse(csvText, {
191191
header: true,
192192
skipEmptyLines: true,
193+
// 【關鍵修正】在解析 CSV 時,對標題進行 trim() 處理,確保欄位名稱匹配準確
194+
transformHeader: function(header) {
195+
return header.trim();
196+
},
193197
complete: function(results) {
194-
// 移除 Google Sheet 自動添加的時間戳記欄位,並過濾掉沒有主要資料的列
198+
195199
rawData = results.data
196200
.map(row => {
197-
// 確保欄位名稱正確對應
201+
// 確保欄位名稱正確對應 (現在 keys 已經被 transformHeader 清理過了)
202+
// 注意:Google 表單導出的 CSV 第一個欄位通常是 '時間戳記',但由於我們使用了 transformHeader
203+
// 這裡可以直接使用我們預期的中文欄位名稱
198204
const {
199205
"發生日期": date,
200206
"發生時間": time,
@@ -212,6 +218,10 @@ <h3 class="text-lg font-semibold text-gray-700 mb-4">異味發生熱點時段 (2
212218
};
213219
})
214220
.filter(row => row.date && row.location); // 確保關鍵欄位有值
221+
222+
if (rawData.length === 0) {
223+
displayError("資料已成功讀取,但未包含有效的回報數據。請檢查試算表內容。");
224+
}
215225

216226
resolve(rawData);
217227
},
@@ -222,7 +232,7 @@ <h3 class="text-lg font-semibold text-gray-700 mb-4">異味發生熱點時段 (2
222232
});
223233
} catch (error) {
224234
console.error("資料獲取或解析錯誤:", error);
225-
displayError(`無法載入或解析數據:${error.message}`);
235+
displayError(`無法載入或解析數據:${error.message} (錯誤碼: ${error.message.includes('Failed to fetch') ? '網路/CORS' : '解析失敗'})`);
226236
return [];
227237
} finally {
228238
loadingOverlay.classList.add('hidden');

0 commit comments

Comments
 (0)