HTML 서비스: HTML 만들기 및 게재
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
HTML 서비스를 사용하면 서버 측 Apps Script 함수와 상호작용할 수 있는 웹페이지를 제공할 수 있습니다. 특히 웹 앱을 빌드하거나 Google Docs, Sheets, Forms에 맞춤 사용자 인터페이스를 추가하는 데 유용합니다. 이메일 본문을 생성하는 데 사용할 수도 있습니다.
HTML 파일 만들기
Apps Script 프로젝트에 HTML 파일을 추가하려면 다음 단계를 따르세요.
- Apps Script 편집기를 엽니다.
- 왼쪽에서 파일 추가 add
> HTML을 클릭합니다.
HTML 파일 내에서 대부분의 표준 HTML, CSS, 클라이언트 측 JavaScript를 작성할 수 있습니다. 이 페이지는 HTML5로 제공되지만 제한사항에 설명된 대로 HTML5의 일부 고급 기능은 사용할 수 없습니다.
템플릿 HTML 섹션에 설명된 대로 PHP와 유사하게 페이지가 사용자에게 전송되기 전에 서버에서 처리되는 템플릿 스크립틀릿을 파일에 포함할 수도 있습니다.
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 사이트 도구에 삽입할 수도 있습니다.
HTML을 Google Docs, Sheets, Slides 또는 Forms 사용자 인터페이스로 제공
스크립트가 파일에 컨테이너 바운드된 경우 HTML 서비스는 Google Docs, Sheets, Slides 또는 Forms에 대화상자 또는 사이드바를 표시할 수 있습니다. (Google Forms에서는 맞춤 사용자 인터페이스가 양식을 수정하기 위해 여는 편집자에게만 표시되며, 양식에 응답하기 위해 여는 사용자에게는 표시되지 않습니다.)
웹 앱과 달리 문서, 스프레드시트 또는 양식의 사용자 인터페이스를 만드는 스크립트에는 doGet()
함수가 특별히 필요하지 않으며 스크립트 버전을 저장하거나 배포할 필요가 없습니다. 대신 사용자 인터페이스를 여는 함수는 활성 문서, 양식 또는 스프레드시트의 Ui
객체의 showModalDialog())
또는 showSidebar()
메서드에 HTML 파일을 HtmlOutput
객체로 전달해야 합니다.
이 예에는 편의를 위해 몇 가지 추가 기능이 포함되어 있습니다. onOpen()
함수는 인터페이스를 쉽게 열 수 있는 맞춤 메뉴를 만들고 HTML 파일의 버튼은 google.script.host
API의 특수 close()
메서드를 호출하여 인터페이스를 닫습니다.
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()
함수를 스크립트 편집기에서 수동으로 실행하거나 Docs, Sheets 또는 Forms 편집기의 창을 새로고침해야 합니다 (스크립트 편집기가 닫힘). 그런 다음 파일을 열 때마다 몇 초 이내에 맞춤 메뉴가 표시됩니다. 대화상자 > 열기를 선택하여 인터페이스를 확인합니다.
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 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."]]