Class HtmlService

HtmlService

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 ofHtmlOutput for what limitations this implies on what can be returned.

Properties

PropertyTypeDescription
SandboxModeSandboxModeAn enum representing the sandbox modes that can be used for client-sideHtmlServicescripts.
XFrameOptionsModeXFrameOptionsModeAn enum representing theX-Frame-Options modes that can be used for client-sideHtmlService scripts.

Methods

MethodReturn typeBrief description
createHtmlOutput()HtmlOutputCreates a newHtmlOutput object that can be returned from the script.
createHtmlOutput(blob)HtmlOutputCreates a newHtmlOutput object from aBlobSource resource.
createHtmlOutput(html)HtmlOutputCreates a newHtmlOutput object that can be returned from the script.
createHtmlOutputFromFile(filename)HtmlOutputCreates a newHtmlOutput object from a file in the code editor.
createTemplate(blob)HtmlTemplateCreates a newHtmlTemplate object from aBlobSource resource.
createTemplate(html)HtmlTemplateCreates a newHtmlTemplate object that can be returned from the script.
createTemplateFromFile(filename)HtmlTemplateCreates a newHtmlTemplate object from a file in the code editor.
getUserAgent()StringGets the user-agent string for the current browser.

Detailed documentation

createHtmlOutput()

Creates a newHtmlOutput object that can be returned from the script.

constoutput=HtmlService.createHtmlOutput();

Return

HtmlOutput — the new HtmlOutput object


createHtmlOutput(blob)

Creates a newHtmlOutput object from aBlobSource resource.

functioncreateFromBlob(blob){constoutput=HtmlService.createHtmlOutput(blob);returnoutput;}

Parameters

NameTypeDescription
blobBlobSourcethe object to get HTML out of

Return

HtmlOutput — the newHtmlOutput object

Throws

Error — if the blob doesn't contain HTML or the HTML is malformed


createHtmlOutput(html)

Creates a newHtmlOutput object that can be returned from the script.

constoutput=HtmlService.createHtmlOutput('<b>Hello world!</b>');

Parameters

NameTypeDescription
htmlStringthe content to serve

Return

HtmlOutput — the new HtmlOutput object

Throws

Error — if the html is malformed


createHtmlOutputFromFile(filename)

Creates a newHtmlOutput object from a file in the code editor.

constoutput=HtmlService.createHtmlOutputFromFile('myPage');

Parameters

NameTypeDescription
filenameStringthe name of the file to use

Return

HtmlOutput — the newHtmlOutput object

Throws

Error — if the file wasn't found or the HTML in it is malformed


createTemplate(blob)

Creates a newHtmlTemplate object from aBlobSource resource.

functioncreateFromBlob(blob){consttemplate=HtmlService.createTemplate(blob);constoutput=template.evaluate();returnoutput;}

Parameters

NameTypeDescription
blobBlobSourceThe object to get HTML out of.

Return

HtmlTemplate — the newHtmlTemplate object

Throws

Error — if the blob doesn't contain HTML


createTemplate(html)

Creates a newHtmlTemplate object that can be returned from the script.

consttemplate=HtmlService.createTemplate('<b>The time is &lt;?= new Date() ?&gt;</b>',);

Parameters

NameTypeDescription
htmlStringthe content of the template

Return

HtmlTemplate — the newHtmlTemplate object


createTemplateFromFile(filename)

Creates a newHtmlTemplate object from a file in the code editor.

consttemplate=HtmlService.createTemplateFromFile('myTemplate');

Parameters

NameTypeDescription
filenameStringthe name of the file to use

Return

HtmlTemplate — the newHtmlTemplate object

Throws

Error — if the file wasn't found


getUserAgent()

Gets the user-agent string for the current browser. Returnsnull for most scriptexecutions if not used in a web app'sdoGet() ordoPost() function.

Return

String — the user-agent string

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2024-12-03 UTC.