티스토리 수익 글 보기
Class HtmlService
Stay organized with collections
Save and categorize content based on your preferences.
Page Summary
-
HtmlService allows scripts to return HTML and other text content to a browser safely through sanitization.
-
Security measures are in place to prevent scripts from directly returning malicious content.
-
The service includes properties like
SandboxModeandXFrameOptionsModefor controlling client-side script behavior and security. -
Methods are available to create
HtmlOutputobjects from various sources, including strings, blobs, and files. -
The service also provides methods for creating
HtmlTemplateobjects for dynamic content generation.
Service for returning HTML and other text content from a script.
Due to security considerations, scripts cannot directly return content to a browser. Instead,
they must sanitize the HTML so that it cannot perform malicious actions. See the description of
HtmlOutput for what limitations this implies on what can be returned.
Properties
| Property | Type | Description |
|---|---|---|
Sandbox | Sandbox | An enum representing the sandbox modes that can be used for client-side Html
scripts. |
XFrameOptionsMode | XFrameOptionsMode | An enum representing the X-Frame-Options modes that can be used for client-side Html scripts. |
Methods
| Method | Return type | Brief description |
|---|---|---|
create | Html | Creates a new Html object that can be returned from the script. |
create | Html | Creates a new Html object from a Blob resource. |
create | Html | Creates a new Html object that can be returned from the script. |
create | Html | Creates a new Html object from a file in the code editor. |
create | Html | Creates a new Html object from a Blob resource. |
create | Html | Creates a new Html object that can be returned from the script. |
create | Html | Creates a new Html object from a file in the code editor. |
get | String | Gets the user-agent string for the current browser. |
Detailed documentation
createHtmlOutput()
Creates a new HtmlOutput object that can be returned from the script.
const output = HtmlService.createHtmlOutput();
Return
HtmlOutput — the new HtmlOutput object
createHtmlOutput(blob)
Creates a new HtmlOutput object from a BlobSource resource.
function createFromBlob(blob) { const output = HtmlService.createHtmlOutput(blob); return output; }
Parameters
| Name | Type | Description |
|---|---|---|
blob | Blob | the object to get HTML out of |
Return
HtmlOutput — the new HtmlOutput object
Throws
Error — if the blob doesn’t contain HTML or the HTML is malformed
createHtmlOutput(html)
Creates a new HtmlOutput object that can be returned from the script.
const output = HtmlService.createHtmlOutput('<b>Hello world!</b>');
Parameters
| Name | Type | Description |
|---|---|---|
html | String | the content to serve |
Return
HtmlOutput — the new HtmlOutput object
Throws
Error — if the html is malformed
createHtmlOutputFromFile(filename)
Creates a new HtmlOutput object from a file in the code editor.
const output = HtmlService.createHtmlOutputFromFile('myPage');
Parameters
| Name | Type | Description |
|---|---|---|
filename | String | the name of the file to use |
Return
HtmlOutput — the new HtmlOutput object
Throws
Error — if the file wasn’t found or the HTML in it is malformed
createTemplate(blob)
Creates a new HtmlTemplate object from a BlobSource resource.
function createFromBlob(blob) { const template = HtmlService.createTemplate(blob); const output = template.evaluate(); return output; }
Parameters
| Name | Type | Description |
|---|---|---|
blob | Blob | The object to get HTML out of. |
Return
HtmlTemplate — the new HtmlTemplate object
Throws
Error — if the blob doesn’t contain HTML
createTemplate(html)
Creates a new HtmlTemplate object that can be returned from the script.
const template = HtmlService.createTemplate( '<b>The time is <?= new Date() ?></b>', );
Parameters
| Name | Type | Description |
|---|---|---|
html | String | the content of the template |
Return
HtmlTemplate — the new HtmlTemplate object
createTemplateFromFile(filename)
Creates a new HtmlTemplate object from a file in the code editor.
const template = HtmlService.createTemplateFromFile('myTemplate');
Parameters
| Name | Type | Description |
|---|---|---|
filename | String | the name of the file to use |
Return
HtmlTemplate — the new HtmlTemplate object
Throws
Error — if the file wasn’t found
getUserAgent()
Gets the user-agent string for the current browser. Returns null for most script
executions if not used in a web app’s doGet() or doPost() function.
Return
String — the user-agent string
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-04-13 UTC.