เทคนิคแก้ปัญหา “ลิงก์ Google Apps Script เปิดไม่ได้จากระบบองค์กร (Google Workspace)"

 


บทนำ

หลายหน่วยงานที่ใช้ Google Apps Script Web App มักพบปัญหาว่า
เมื่อเปิดลิงก์จาก Gmail / Google Workspace / ระบบองค์กร จะเกิดอาการ:

  • หน้าเว็บไม่โหลด
  • ถูกบล็อกด้วยระบบความปลอดภัย
  • Redirect ไม่สมบูรณ์
  • เปิดได้เฉพาะบางเบราว์เซอร์

บทความนี้จะแนะนำ เทคนิค Safe Redirect + Healing Page
เพื่อให้ Web App ของเรา เปิดได้เสถียรและปลอดภัย ในทุกบริบทของ Google Workspace


สาเหตุของปัญหา

เมื่อผู้ใช้คลิกลิงก์ Web App จาก:

  • Gmail
  • Google Chat
  • ระบบภายในองค์กร

ลิงก์มักถูกเปิดผ่านโดเมนกลาง เช่น

googleusercontent.com


ซึ่ง Google จะเพิ่มชั้นความปลอดภัย (Sandbox / Redirect Layer)
ทำให้ Apps Script Web App บางตัวไม่สามารถทำงานได้เต็มรูปแบบ


💡 แนวคิดการแก้ไข (Solution Concept)

เราสร้างหน้า “ตัวกลางที่ปลอดภัย” (Healing Page) โดยใช้ doGet() ทำหน้าที่:

  1. ตรวจสอบแหล่งที่มา (Referrer / User Agent)
  2. ตรวจสอบ Host ที่มีความเสี่ยง
  3. Redirect ไปยัง Web App จริงแบบอัตโนมัติ
  4. ป้องกัน Redirect Loop
  5. แสดง UI ระหว่างรอ (Loading)


โครงสร้างการทำงาน

ผู้ใช้คลิกลิงก์ ↓ Healing Page (doGet) ↓ ตรวจสอบบริบท ↓ Redirect ไป Web App หลัก

ตัวอย่างโค้ด Google Apps Script

function doGet(e) {
// --- ตั้งค่า URL ของคุณที่นี่ --- const SAFE_URL = "https://script.google.com/macros/s/AKfycb...IT5TI4kej6kQHdp/exec"; return HtmlService.createHtmlOutput(` <!DOCTYPE html> <html lang="th"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="refresh" content="5;url=${SAFE_URL}"> <style> body { font-family: sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background: #f9f9f9; } .card { padding: 2rem; text-align: center; } .spinner { border: 4px solid #f3f3f3; border-top: 4px solid #3498db; border-radius: 50%; width: 30px; height: 30px; animation: spin 1s linear infinite; margin: 0 auto 15px; } @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } </style> </head> <body> <div class="card"> <div id="loader" class="spinner"></div> <p id="status">กำลังเชื่อมต่อระบบปลอดภัย...</p> </div> <script> (function () { const targetUrl = "${SAFE_URL}"; const host = location.hostname; const ref = document.referrer.toLowerCase(); const ua = navigator.userAgent.toLowerCase(); const healed = sessionStorage.getItem("healed"); const isWorkspace = ref.includes("mail.google.com") || ua.includes("gmail") || ua.includes("workspace"); const isRiskHost = host.includes("googleusercontent.com"); if (isWorkspace && isRiskHost && !healed) { sessionStorage.setItem("healed", "1"); window.location.replace(targetUrl); return; } document.getElementById("status").innerHTML = "⏳ กำลังโหลดหน้าเว็บ..."; })(); </script> </body> </html> `); }

จุดเด่นของวิธีนี้

✅ ใช้ได้กับทุกหน่วยงาน
✅ รองรับ Gmail / Workspace / Browser ทั่วไป
✅ ไม่ต้องตั้งค่า Admin เพิ่ม
✅ ป้องกัน Redirect Loop
✅ UX ดี มี Loading Indicator
✅ ใช้ได้เป็น Template กลาง


เหมาะสำหรับใคร?

  • หน่วยงานราชการ / องค์กรใหญ่
  • ระบบที่เปิดผ่านอีเมล
  • Google Apps Script Web App
  • ระบบ LMS / Booking / Dashboard
  • ผู้พัฒนา GAS ที่เจอปัญหา “เปิดลิงก์ไม่ได้ในองค์กร”


สรุป

เทคนิค Healing Redirect Page
เป็นวิธีที่เรียบง่าย แต่ทรงพลังมากในการแก้ปัญหา Web App เปิดไม่ได้จากระบบองค์กร

👉 “ไม่ต้องแก้ที่ผู้ใช้ ไม่ต้องแก้ที่ Admin — แก้ที่โค้ดเรา”


 หวังว่าเทคนิคนี้จะช่วยให้ Web App ของเพื่อนๆ ทำงานได้อย่างราบรื่นขึ้นนะครับ! หากใครนำไปใช้แล้วได้ผลอย่างไร หรือมีวิธีที่เจ๋งกว่านี้ มาแชร์กันได้ในคอมเมนต์ครับ



แสดงความคิดเห็น (0)
ใหม่กว่า เก่ากว่า