บริการ HTML: สร้างและแสดงผล HTML
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
บริการ HTML ช่วยให้คุณแสดงหน้าเว็บที่โต้ตอบกับฟังก์ชัน Apps Script ฝั่งเซิร์ฟเวอร์ได้ โดยเฉพาะอย่างยิ่งมีประโยชน์
สำหรับการสร้างเว็บแอปหรือการเพิ่มอินเทอร์เฟซผู้ใช้ที่กำหนดเองใน Google เอกสาร, ชีต
และฟอร์ม คุณยังใช้ฟีเจอร์นี้เพื่อสร้างเนื้อหาของอีเมลได้ด้วย
สร้างไฟล์ HTML
หากต้องการเพิ่มไฟล์ HTML ลงในโปรเจ็กต์ Apps Script ให้ทำตามขั้นตอนต่อไปนี้
- เปิดเครื่องมือแก้ไข Apps Script
- ที่ด้านซ้าย ให้คลิกเพิ่มไฟล์ add
> HTML
คุณเขียน HTML, CSS และ JavaScript ฝั่งไคลเอ็นต์มาตรฐานส่วนใหญ่ได้ภายในไฟล์ HTML
ระบบจะแสดงหน้าเว็บเป็น HTML5 แม้ว่าฟีเจอร์ขั้นสูงบางอย่างของ
HTML5 จะไม่พร้อมใช้งานตามที่อธิบายไว้ใน
ข้อจำกัด
นอกจากนี้ ไฟล์ยังอาจมีสคริปต์ย่อยของเทมเพลตที่ประมวลผลในเซิร์ฟเวอร์
ก่อนที่จะส่งหน้าเว็บไปยังผู้ใช้ ซึ่งคล้ายกับ PHP ดังที่อธิบายไว้ในส่วนHTML ที่ใช้เทมเพลต
แสดง HTML เป็นเว็บแอป
หากต้องการสร้างเว็บแอปด้วยบริการ HTML โค้ดของคุณต้องมีdoGet()
ฟังก์ชันที่บอกสคริปต์วิธีแสดงหน้าเว็บ ฟังก์ชันต้องแสดงผลออบเจ็กต์ HtmlOutput
ดังที่แสดงใน
ตัวอย่างนี้
Code.gs
function doGet() {
return HtmlService.createHtmlOutputFromFile('Index');
}
Index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
Hello, World!
</body>
</html>
เมื่อมีเฟรมเวิร์กพื้นฐานแล้ว สิ่งที่คุณต้องทำก็คือ
บันทึกเวอร์ชันของสคริปต์ จากนั้น
นําสคริปต์ไปใช้เป็นเว็บแอป
หลังจากที่ติดตั้งใช้งานสคริปต์เป็นเว็บแอปแล้ว คุณยังฝังสคริปต์ใน Google Sites ได้ด้วย
แสดง HTML เป็นอินเทอร์เฟซผู้ใช้ของ Google เอกสาร, ชีต, สไลด์ หรือฟอร์ม
บริการ HTML สามารถแสดงกล่องโต้ตอบหรือแถบด้านข้าง
ใน Google เอกสาร ชีต สไลด์ หรือฟอร์มได้หากสคริปต์เชื่อมโยงกับคอนเทนเนอร์ของไฟล์ (ใน Google ฟอร์ม
อินเทอร์เฟซผู้ใช้ที่กำหนดเองจะแสดงต่อเฉพาะผู้แก้ไขที่เปิดแบบฟอร์มเพื่อ
แก้ไขเท่านั้น ไม่แสดงต่อผู้ใช้ที่เปิดแบบฟอร์มเพื่อตอบ)
ไม่เหมือนกับเว็บแอป สคริปต์ที่สร้างอินเทอร์เฟซผู้ใช้สำหรับเอกสาร
สเปรดชีต หรือแบบฟอร์มไม่จำเป็นต้องมีฟังก์ชัน doGet()
โดยเฉพาะ และคุณไม่จำเป็นต้องบันทึกเวอร์ชันของสคริปต์หรือติดตั้งใช้งาน แต่ฟังก์ชัน
ที่เปิดอินเทอร์เฟซผู้ใช้ต้องส่งไฟล์ HTML เป็นออบเจ็กต์
HtmlOutput
ไปยังเมธอด
showModalDialog())
หรือ showSidebar()
ของออบเจ็กต์
Ui
สำหรับเอกสาร ฟอร์ม หรือ
สเปรดชีตที่ใช้งานอยู่
ตัวอย่างเหล่านี้มีฟีเจอร์เพิ่มเติมเพื่อความสะดวก ได้แก่ onOpen()
ฟังก์ชันสร้างเมนูที่กำหนดเองซึ่งช่วยให้เปิดอินเทอร์เฟซได้ง่าย
และปุ่มในไฟล์ HTML จะเรียกใช้เมธอดพิเศษclose()
ของ API google.script.host
เพื่อปิดอินเทอร์เฟซ
Code.gs
// Use this code for Google Docs, Slides, Forms, or Sheets.
function onOpen() {
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.createMenu('Dialog')
.addItem('Open', 'openDialog')
.addToUi();
}
function openDialog() {
var html = HtmlService.createHtmlOutputFromFile('Index');
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.showModalDialog(html, 'Dialog title');
}
Index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
Hello, World!
<input type="button" value="Close"
onclick="google.script.host.close()" />
</body>
</html>
โปรดทราบว่าครั้งแรกที่คุณต้องการแสดงอินเทอร์เฟซผู้ใช้นี้ คุณต้อง
เรียกใช้ฟังก์ชัน onOpen()
ด้วยตนเองในตัวแก้ไขสคริปต์
หรือโหลดหน้าต่างสำหรับตัวแก้ไขเอกสาร ชีต หรือฟอร์มซ้ำ (ซึ่งจะปิดตัวแก้ไขสคริปต์) หลังจากนั้น เมนูที่กำหนดเองจะปรากฏภายในไม่กี่วินาที
ทุกครั้งที่คุณเปิดไฟล์ เลือกกล่องโต้ตอบ > เปิดเพื่อดู
อินเทอร์เฟซ
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-31 UTC
[null,null,["อัปเดตล่าสุด 2025-08-31 UTC"],[[["\u003cp\u003eThe HTML service allows you to create web pages that interact with Apps Script functions, enabling you to build web apps or add custom interfaces to Google Docs, Sheets, and Forms.\u003c/p\u003e\n"],["\u003cp\u003eYou can build HTML files within your Apps Script project using standard HTML, CSS, and client-side JavaScript, enhancing the functionality of your applications.\u003c/p\u003e\n"],["\u003cp\u003eDeploying your script as a web app allows external access, or it can be embedded in a Google Site for integration with your existing web presence.\u003c/p\u003e\n"],["\u003cp\u003eFor Google Docs, Sheets, Slides, or Forms, the HTML service enables the creation of custom dialogs or sidebars, providing interactive elements within these applications.\u003c/p\u003e\n"],["\u003cp\u003eContainer-bound scripts can display user interfaces using the HTML service by invoking \u003ccode\u003eshowModalDialog()\u003c/code\u003e or \u003ccode\u003eshowSidebar()\u003c/code\u003e methods, offering a tailored user experience.\u003c/p\u003e\n"]]],[],null,["# HTML Service: Create and Serve HTML\n\nThe [HTML service](/apps-script/reference/html) lets you serve web pages that\ncan interact with server-side Apps Script functions. It is particularly useful\nfor building web apps or adding custom user interfaces in Google Docs, Sheets,\nand Forms. You can even use it to generate the body of an email.\n\nCreate HTML files\n-----------------\n\nTo add an HTML file to your Apps Script project, follow these steps:\n\n1. Open the Apps Script editor.\n2. At the left, click Add a file add \\\u003e **HTML**.\n\nWithin the HTML file, you can write most standard HTML, CSS, and client-side\nJavaScript. The page will be served as HTML5, although some advanced features of\nHTML5 are not available, as explained in\n[Restrictions](/apps-script/guides/html/restrictions).\n\nYour file can also include template scriptlets that are processed on the server\nbefore the page is sent to the user --- similar to PHP --- as explained in the\nsection on [templated HTML](/apps-script/guides/html/templates).\n\nServe HTML as a web app\n-----------------------\n\nTo create a web app with the HTML service, your code must include a `doGet()`\nfunction that tells the script how to serve the page. The function must return\nan [`HtmlOutput`](/apps-script/reference/html/html-output) object, as shown in\nthis example. \n\n### Code.gs\n\n```html\nfunction doGet() {\n return HtmlService.createHtmlOutputFromFile('Index');\n}\n```\n\n### Index.html\n\n```html\n\u003c!DOCTYPE html\u003e\n\u003chtml\u003e\n \u003chead\u003e\n \u003cbase target=\"_top\"\u003e\n \u003c/head\u003e\n \u003cbody\u003e\n Hello, World!\n \u003c/body\u003e\n\u003c/html\u003e\n```\n\nOnce that basic framework is in place, all you have to do is\n[save a version of your script](/apps-script/guides/versions), then\n[deploy your script as a web app](/apps-script/execution_web_apps#deploying).\n\nAfter the script is deployed as a web app, you can also\n[embed it in a Google Site](/apps-script/guides/web#embed_your_web_app_in).\n\nServe HTML as a Google Docs, Sheets, Slides, or Forms user interface\n--------------------------------------------------------------------\n\nThe HTML service can display a [dialog or sidebar](/apps-script/guides/dialogs)\nin Google Docs, Sheets, Slides, or Forms if your script is\n[container-bound](/apps-script/guides/bound) to the file. (In Google Forms,\ncustom user interfaces are only visible to an editor who opens the form to\nmodify it, not to a user who opens the form to respond.)\n\nUnlike a web app, a script that creates a user interface for a document,\nspreadsheet, or form does not need a `doGet()` function specifically, and you do\nnot need to save a version of your script or deploy it. Instead, the function\nthat opens the user interface must pass your HTML file as an\n[`HtmlOutput`](/apps-script/reference/html/html-output) object to the\n`showModalDialog())` or `showSidebar()` methods of the\n[`Ui`](/apps-script/reference/base/ui) object for the active document, form, or\nspreadsheet.\n\nThese examples include a few extra features for convenience: the `onOpen()`\nfunction creates a [custom menu](/apps-script/guides/menus) that makes it easy\nto open the interface, and the button in the HTML file invokes a special\n`close()` method of the\n[`google.script.host`](/apps-script/guides/html/communication) API to close the\ninterface. \n\n### Code.gs\n\n```html\n// Use this code for Google Docs, Slides, Forms, or Sheets.\nfunction onOpen() {\n SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.\n .createMenu('Dialog')\n .addItem('Open', 'openDialog')\n .addToUi();\n}\n\nfunction openDialog() {\n var html = HtmlService.createHtmlOutputFromFile('Index');\n SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.\n .showModalDialog(html, 'Dialog title');\n}\n```\n\n### Index.html\n\n```html\n\u003c!DOCTYPE html\u003e\n\u003chtml\u003e\n \u003chead\u003e\n \u003cbase target=\"_top\"\u003e\n \u003c/head\u003e\n \u003cbody\u003e\n Hello, World!\n \u003cinput type=\"button\" value=\"Close\"\n onclick=\"google.script.host.close()\" /\u003e\n \u003c/body\u003e\n\u003c/html\u003e\n```\n\nNote that the first time you want to display this user interface, you must\neither run the `onOpen()` function\n[manually in the script editor](/apps-script/execution_script_editor)\nor reload the window for the Docs, Sheets, or Forms editor (which will close the\nscript editor). After that, the custom menu should appear within a few seconds\nevery time you open the file. Choose **Dialog \\\u003e Open** to see the\ninterface."]]