Google Apps Script (GAS) 部署程式碼檔
部署步驟:開啟 Google 試算表 → 點選選單「擴充功能」 → 「Apps Script」 → 清空原內容並貼上以下程式碼 → 點擊右上「部署」 → 「新增部署」 → 選擇「網頁應用程式 (Web App)」 → 將誰可以存取設為「所有人 (Anyone)」 → 複製獲得的 Web App 網址貼於上方!
function doPost(e) {
try {
var data = JSON.parse(e.postData.contents);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("甲醛檢驗歷史紀錄") || ss.insertSheet("甲醛檢驗歷史紀錄");
// 建立表頭
if (sheet.getLastRow() === 0) {
sheet.appendRow([
"同步時間", "分析日期", "檢體序號", "檢體類別",
"理論濃度(μg/mL)", "理論含量(mg/kg)", "取樣重量(g)", "定容體積(mL)",
"分光吸光值", "HPLC面積", "分光計算濃度", "分光計算含量",
"分光結果", "HPLC計算濃度", "HPLC計算含量", "HPLC結果"
]);
sheet.getRange(1, 1, 1, 16).setFontWeight("bold").setBackground("#f1f5f9");
}
var timestamp = new Date().toLocaleString("zh-TW", {timeZone: "Asia/Taipei"});
data.rows.forEach(function(r) {
sheet.appendRow([
timestamp,
r.date || data.date,
r.id,
r.type,
r.conc_theory !== null && r.conc_theory !== undefined ? r.conc_theory : "",
r.content_theory !== null && r.content_theory !== undefined ? r.content_theory : "",
r.weight !== null && r.weight !== undefined ? r.weight : "",
r.volume !== null && r.volume !== undefined ? r.volume : "",
r.spectro_abs !== null && r.spectro_abs !== undefined ? r.spectro_abs : "",
r.hplc_area !== null && r.hplc_area !== undefined ? r.hplc_area : "",
r.conc_calc !== undefined && r.conc_calc !== null ? r.conc_calc : "",
r.content_calc !== undefined && r.content_calc !== null ? r.content_calc : "",
r.final_text || "",
r.hplc_conc_calc !== undefined && r.hplc_conc_calc !== null ? r.hplc_conc_calc : "",
r.hplc_content_calc !== undefined && r.hplc_content_calc !== null ? r.hplc_content_calc : "",
r.hplc_final_text || ""
]);
});
return ContentService.createTextOutput(JSON.stringify({result: "success"}))
.setMimeType(ContentService.MimeType.JSON);
} catch(err) {
return ContentService.createTextOutput(JSON.stringify({result: "error", error: err.toString()}))
.setMimeType(ContentService.MimeType.JSON);
}
}