บริการ HTML: ข้อจำกัด
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
Apps Script ใช้ iframe เพื่อจำกัดการเข้าถึงเว็บแอปที่ใช้บริการ HTML หรืออินเทอร์เฟซผู้ใช้ที่กำหนดเองสำหรับ Google เอกสาร, ชีต และฟอร์ม เพื่อปกป้องผู้ใช้จากการแสดง HTML หรือ JavaScript ที่เป็นอันตราย
(บริการ HTML ไม่ได้ใช้
แซนด์บ็อกซ์ในสถานการณ์อื่นๆ เช่น การสร้างเนื้อหาของอีเมล) แซนด์บ็อกซ์
กำหนดข้อจำกัดเกี่ยวกับโค้ดฝั่งไคลเอ็นต์
โหมดแซนด์บ็อกซ์
ตอนนี้เราได้หยุดให้บริการโหมดแซนด์บ็อกซ์ทั้งหมดแล้ว ยกเว้น IFRAME
แอปที่ใช้ Sandbox
โหมดเก่าจะใช้โหมด IFRAME
ใหม่กว่าโดยอัตโนมัติ หากมีสคริปต์ที่พัฒนาโดยใช้โหมดเก่า (NATIVE
และ EMULATED
) คุณควรทำตามวิธีการย้ายข้อมูลเพื่อให้แน่ใจว่าสคริปต์ทำงานได้อย่างถูกต้องในโหมด IFRAME
ตอนนี้เมธอด setSandboxMode
จะไม่มีผลเมื่อเรียกใช้
ข้อจำกัดในโหมด IFRAME
IFRAME
โหมดแซนด์บ็อกซ์อิงตามฟีเจอร์
แซนด์บ็อกซ์ iframe
ใน HTML5 โดยใช้คีย์เวิร์ดต่อไปนี้
allow-same-origin
allow-forms
allow-scripts
allow-popups
allow-downloads
allow-modals
allow-popups-to-escape-sandbox
allow-top-navigation-by-user-activation
- แอตทริบิวต์นี้จะตั้งค่าสำหรับโปรเจ็กต์สคริปต์แบบสแตนด์อโลนเท่านั้น
คีย์เวิร์ด allow-top-navigation
ซึ่งช่วยให้เนื้อหานำทางไปยังบริบทการเรียกดูระดับบนสุดได้จะถูกจำกัดและไม่ได้ตั้งค่าเป็นแอตทริบิวต์ในแซนด์บ็อกซ์ หากต้องการเปลี่ยนเส้นทางสคริปต์ ให้เพิ่มลิงก์หรือปุ่มเพื่อให้
ผู้ใช้ดำเนินการแทน
การตั้งค่าแอตทริบิวต์เป้าหมายลิงก์
ในโหมด IFRAME
คุณต้องตั้งค่าแอตทริบิวต์เป้าหมายของลิงก์เป็น
_top
หรือ _blank
Code.js
function doGet() {
var template = HtmlService.createTemplateFromFile('top');
return template.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
top.html
<!DOCTYPE html>
<html>
<body>
<div>
<a href="http://google.com" target="_top">Click Me!</a>
</div>
</body>
</html>
นอกจากนี้ คุณยังลบล้างแอตทริบิวต์นี้ได้โดยใช้แท็ก <base>
ภายในส่วน head
ของหน้าเว็บที่ครอบคลุม
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<div>
<a href="http://google.com">Click Me!</a>
</div>
</body>
</html>
ต้องใช้ HTTPS สำหรับเนื้อหาที่ใช้งานอยู่
เนื้อหา"ที่ใช้งานอยู่"
เช่น สคริปต์ สไตล์ชีตภายนอก และ XmlHttpRequest ต้องโหลดผ่าน
HTTPS ไม่ใช่ HTTP
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-31 UTC
[null,null,["อัปเดตล่าสุด 2025-08-31 UTC"],[[["\u003cp\u003eApps Script utilizes iframes to sandbox HTML service web apps and custom interfaces for Google products, protecting users from malicious code.\u003c/p\u003e\n"],["\u003cp\u003eAll sandbox modes except \u003ccode\u003eIFRAME\u003c/code\u003e are sunset; existing scripts using older modes automatically transition to \u003ccode\u003eIFRAME\u003c/code\u003e, and developers should migrate their code accordingly.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eIFRAME\u003c/code\u003e sandbox mode, based on HTML5 iframe sandboxing, imposes limitations on client-side code with specific allowances and restrictions.\u003c/p\u003e\n"],["\u003cp\u003eTo redirect users in \u003ccode\u003eIFRAME\u003c/code\u003e mode, use links or buttons with \u003ccode\u003etarget="_top"\u003c/code\u003e or \u003ccode\u003e_blank"\u003c/code\u003e or utilize the \u003ccode\u003e<base>\u003c/code\u003e tag to override this behavior.\u003c/p\u003e\n"],["\u003cp\u003eActive content like scripts and external stylesheets within the sandboxed environment must be loaded over HTTPS to ensure security.\u003c/p\u003e\n"]]],[],null,["# HTML Service: Restrictions\n\nTo protect users from being served malicious HTML or JavaScript, Apps Script\nuses iframes to sandbox HTML-service web apps or custom user\ninterfaces for Google Docs, Sheets, and Forms. (The HTML service does not use a\nsandbox in other situations, like generating the body of an email.) The sandbox\nimposes limitations on client-side code.\n\nSandbox Mode\n------------\n\nAll sandbox modes are now sunset except for `IFRAME`. Apps using older sandbox\nmodes now use the newer `IFRAME` mode automatically. If you have scripts that\nwere developed using the older modes (`NATIVE` and `EMULATED`), you should\nfollow the [migration instructions](/apps-script/migration/iframe) to ensure\nthey function properly under the `IFRAME` mode.\n\nThe [`setSandboxMode`](/apps-script/reference/html/html-output#setSandboxMode(SandboxMode))\nmethod now has no effect when called.\n\nRestrictions in IFRAME mode\n---------------------------\n\nThe `IFRAME` sandbox mode is based on the\n[iframe sandboxing](https://html.spec.whatwg.org/#attr-iframe-sandbox) feature\nin HTML5, using the following keywords:\n\n- `allow-same-origin`\n- `allow-forms`\n- `allow-scripts`\n- `allow-popups`\n- `allow-downloads`\n- `allow-modals`\n- `allow-popups-to-escape-sandbox`\n- `allow-top-navigation-by-user-activation` - This attribute is only set for [stand-alone script projects](/apps-script/guides/standalone).\n\nThe `allow-top-navigation` keyword, which allows the content to navigate its\ntop-level browsing context, is restricted and not set as an attribute in the\nsandbox. If you need to redirect your script, add a link or a button for the\nuser to take action on instead.\n\n### Setting the link target attribute\n\nIn the `IFRAME` mode you need to set the link target attribute to either\n`_top` or `_blank`: \n\n### Code.js\n\n function doGet() {\n var template = HtmlService.createTemplateFromFile('top');\n return template.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);\n }\n\n### top.html\n\n \u003c!DOCTYPE html\u003e\n \u003chtml\u003e\n \u003cbody\u003e\n \u003cdiv\u003e\n \u003ca href=\"http://google.com\" target=\"_top\"\u003eClick Me!\u003c/a\u003e\n \u003c/div\u003e\n \u003c/body\u003e\n \u003c/html\u003e\n\nYou can also override this attribute using the `\u003cbase\u003e` tag within the head\nsection of the enclosing web page: \n\n \u003c!DOCTYPE html\u003e\n \u003chtml\u003e\n \u003chead\u003e\n \u003cbase target=\"_top\"\u003e\n \u003c/head\u003e\n \u003cbody\u003e\n \u003cdiv\u003e\n \u003ca href=\"http://google.com\"\u003eClick Me!\u003c/a\u003e\n \u003c/div\u003e\n \u003c/body\u003e\n \u003c/html\u003e\n\n### HTTPS required for active content\n\n[\"Active\" content](https://developer.mozilla.org/en-US/docs/Security/MixedContent#Mixed_active_content)\nlike scripts, external stylesheets, and XmlHttpRequests must be loaded over\nHTTPS, not HTTP."]]