Google Forms ใช้ง่าย แต่ปรับหน้าตาและเงื่อนไขได้จำกัด ถ้าอยากได้ฟอร์มที่ ออกแบบเองได้ ตรวจข้อมูลได้ตามต้องการ และบันทึกลงชีตที่เรากำหนด Apps Script Web App คือคำตอบ บทนี้เป็นพื้นฐานที่ทุกระบบในซีรีส์ต่อจากนี้จะใช้
ถ้ายังไม่เคยทำ Web App เลย อ่าน EP14 เปลี่ยน Google Sheets เป็น Web App ก่อนจะเข้าใจง่ายขึ้น
สิ่งที่จะได้
- ฟอร์มกรอก ชื่อ เบอร์โทร หน่วยงาน ใช้งานบนมือถือได้
- ตรวจข้อมูลทั้งฝั่งหน้าเว็บและฝั่งเซิร์ฟเวอร์
- บันทึกลงชีต
Dataพร้อมเวลา ไม่ชนกันแม้หลายคนกดพร้อมกัน
ขั้นที่ 1: เตรียมชีต
- สร้าง Google Sheets ใหม่ เปลี่ยนชื่อแท็บเป็น
Data - แถวที่ 1 ใส่หัวตาราง:
เวลา | ชื่อ | เบอร์โทร | หน่วยงาน - เมนู ส่วนขยาย → Apps Script
ขั้นที่ 2: โค้ดฝั่งเซิร์ฟเวอร์ (Code.gs)
const SHEET_NAME = 'Data';
function doGet() {
return HtmlService.createHtmlOutputFromFile('Index')
.setTitle('แบบฟอร์มบันทึกข้อมูล')
.addMetaTag('viewport', 'width=device-width, initial-scale=1');
}
// กันสูตรแปลกปลอม (ข้อความขึ้นต้นด้วย = + - @) และตัดช่องว่าง
function clean_(v) {
v = String(v == null ? '' : v).trim();
return /^[=+\-@]/.test(v) ? "'" + v : v;
}
function saveRecord(form) {
const name = clean_(form.name);
const phone = String(form.phone || '').replace(/\D/g, '');
const dept = clean_(form.dept);
if (!name) throw new Error('กรุณากรอกชื่อ');
if (!/^0\d{8,9}$/.test(phone)) throw new Error('เบอร์โทรต้องเป็นตัวเลข 9–10 หลัก ขึ้นต้นด้วย 0');
if (!dept) throw new Error('กรุณาเลือกหน่วยงาน');
const lock = LockService.getScriptLock();
lock.waitLock(10000); // รอคิวสูงสุด 10 วินาที
try {
const sh = SpreadsheetApp.getActive().getSheetByName(SHEET_NAME);
sh.appendRow([new Date(), name, "'" + phone, dept]); // ' เก็บเลข 0 นำหน้า
} finally {
lock.releaseLock();
}
return 'บันทึกเรียบร้อย ขอบคุณครับ';
}
ทำไมต้องตรวจซ้ำฝั่งเซิร์ฟเวอร์ — การตรวจในหน้าเว็บช่วยให้ผู้ใช้สะดวก แต่คนที่รู้วิธีสามารถข้ามได้ การตรวจใน saveRecord คือด่านจริง
ขั้นที่ 3: หน้าเว็บ (Index.html)
กด + → HTML ตั้งชื่อ Index แล้ววางโค้ด
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<style>
body{font-family:Tahoma,sans-serif;max-width:480px;margin:24px auto;padding:0 16px}
label{display:block;margin-top:14px;font-weight:bold}
input,select,button{width:100%;padding:10px;margin-top:6px;font-size:16px;box-sizing:border-box}
button{background:#0f8a4f;color:#fff;border:0;border-radius:8px;margin-top:20px}
button:disabled{opacity:.6}
#msg{margin-top:14px}
</style>
</head>
<body>
<h2>แบบฟอร์มบันทึกข้อมูล</h2>
<form id="f">
<label>ชื่อ-สกุล</label>
<input name="name" required maxlength="100">
<label>เบอร์โทร</label>
<input name="phone" inputmode="numeric" required pattern="0[0-9]{8,9}">
<label>หน่วยงาน</label>
<select name="dept" required>
<option value="">-- เลือก --</option>
<option>ฝ่ายบริหาร</option>
<option>ฝ่ายการเงิน</option>
<option>ฝ่ายบริการ</option>
</select>
<button id="btn">บันทึก</button>
</form>
<div id="msg"></div>
<script>
const f = document.getElementById('f');
const btn = document.getElementById('btn');
const msg = document.getElementById('msg');
f.addEventListener('submit', function (e) {
e.preventDefault();
btn.disabled = true; btn.textContent = 'กำลังบันทึก...';
google.script.run
.withSuccessHandler(function (text) {
msg.style.color = 'green'; msg.textContent = text;
f.reset(); btn.disabled = false; btn.textContent = 'บันทึก';
})
.withFailureHandler(function (err) {
msg.style.color = 'crimson'; msg.textContent = err.message;
btn.disabled = false; btn.textContent = 'บันทึก';
})
.saveRecord(f); // ส่งทั้งฟอร์ม ค่าจะอ้างอิงตาม name
});
</script>
</body>
</html>
ขั้นที่ 4: Deploy
- กด ทำให้ใช้งานได้ (Deploy) → การทำให้ใช้งานได้รายการใหม่
- เลือกประเภท เว็บแอป
- ดำเนินการในฐานะ: ตัวฉัน (ผู้ใช้ไม่ต้องมีสิทธิ์เข้าชีต)
- ผู้ที่มีสิทธิ์เข้าถึง: เลือกตามการใช้งาน — “ทุกคน” (ไม่ต้องล็อกอิน) หรือ “ทุกคนที่มีบัญชี Google”
- ครั้งแรกจะขอสิทธิ์ ให้กดอนุญาตด้วยบัญชีของเรา แล้วคัดลอก URL ที่ลงท้ายด้วย
/exec
แก้โค้ดแล้วต้อง จัดการการทำให้ใช้งานได้ → แก้ไข → เวอร์ชันใหม่ URL
/execจึงจะเห็นโค้ดล่าสุด
ปัญหาที่พบบ่อย
| อาการ | สาเหตุ / วิธีแก้ |
|---|---|
| เบอร์โทรเลข 0 หาย | ต้องมี "'" + นำหน้าตอนบันทึก หรือตั้งคอลัมน์เป็นข้อความธรรมดา |
| กดแล้วเงียบ | เปิด Console (F12) ดู error หรือดู การดำเนินการ ใน Apps Script |
Cannot read properties of null | ชื่อแท็บไม่ตรง SHEET_NAME |
| แก้โค้ดแล้วหน้าเว็บไม่เปลี่ยน | ยังไม่ได้ Deploy เวอร์ชันใหม่ |
ต่อยอด
ตอนหน้า EP22 จะทำหน้าค้นหาข้อมูลจากชีตนี้ และถ้าอยากให้ส่ง PDF ทางอีเมลหลังบันทึก ดู EP18





ใส่ความเห็น