An HtmlOutput
object that can be served from a script. Due to security considerations,
scripts cannot directly return HTML to a browser. Instead, they must sanitize it so that it
cannot perform malicious actions. You can return sanitized HTML like this:
function doGet() {
return HtmlService.createHtmlOutput('<b>Hello, world!</b>');
}
The code in the HtmlOutput
can include embedded JavaScript and CSS. (This is standard
client-side JavaScript that manipulates the DOM, not Apps Script). All of this content is
sanitized using Google Caja, which applies
some limitations to your client-side code. For more information, see the
guide to restrictions in HTML service.Methods
Method | Return type | Brief description |
---|---|---|
append(addedContent) | HtmlOutput | Appends new content to the content of this HtmlOutput . |
appendUntrusted(addedContent) | HtmlOutput | Appends new content to the content of this HtmlOutput , using contextual escaping. |
asTemplate() | HtmlTemplate | Returns an HtmlTemplate backed by this HtmlOutput . |
clear() | HtmlOutput | Clears the current content. |
getAs(contentType) | Blob | Return the data inside this object as a blob converted to the specified content type. |
getBlob() | Blob | Return the data inside this object as a blob. |
getContent() | String | Gets the content of this HtmlOutput . |
getHeight() | Integer | Gets the initial height of the custom dialog in Google Docs, Sheets, or Forms. |
getTitle() | String | Gets the title of the output page. |
getWidth() | Integer | Gets the initial width of the custom dialog or sidebar in Google Docs, Sheets, or Forms. |
setContent(content) | HtmlOutput | Sets the content of this HtmlOutput . |
setHeight(height) | HtmlOutput | Sets the initial height of the custom dialog in Google Docs, Sheets, or Forms. |
setSandboxMode(mode) | HtmlOutput | Sets the sandbox mode used for client-side scripts. |
setTitle(title) | HtmlOutput | Sets the title of the output page. |
setWidth(width) | HtmlOutput | Sets the initial width of a custom dialog or sidebar in Google Docs, Sheets, or Forms. |
Detailed documentation
append(addedContent)
Appends new content to the content of this HtmlOutput
. Use this only for content from a
trusted source, because it is not escaped.
// Log "<b>Hello, world!</b><p>Hello again, world.</p>"
var output = HtmlService.createHtmlOutput('<b>Hello, world!</b>');
output.append('<p>Hello again, world.</p>');
Logger.log(output.getContent());
Parameters
Name | Type | Description |
---|---|---|
addedContent | String | the content to append |
Return
HtmlOutput
— this HtmlOutput
itself, useful for chaining
See also
appendUntrusted(addedContent)
Appends new content to the content of this HtmlOutput
, using contextual escaping.
This method will correctly escape content based on the current state of the
HtmlOutput
, so that the result will be a safe string with no markup or side affects.
Use this instead of using append whenever you are adding content from an untrusted source, such
as from a user, to avoid accidentally allowing a cross site scripting (XSS) bug where content
or markup that you append causes unexpected code execution.
// Log "<b>Hello, world!</b><p>Hello again, world.</p>"
var output = HtmlService.createHtmlOutput('<b>Hello, world!</b>');
output.appendUntrusted('<p>Hello again, world.</p>');
Logger.log(output.getContent());
Parameters
Name | Type | Description |
---|---|---|
addedContent | String | the content to append |
Return
HtmlOutput
— this HtmlOutput
itself, useful for chaining
See also
asTemplate()
Returns an HtmlTemplate
backed by this HtmlOutput
. This method can be used to
build up a template incrementally. Future changes to HtmlOutput
will affect the
contents of the HtmlTemplate
as well.
var output = HtmlService.createHtmlOutput('<b>Hello, world!</b>');
var template = output.asTemplate();
Return
HtmlTemplate
— the new HtmlTemplate
clear()
Clears the current content.
var output = HtmlService.createHtmlOutput('<b>Hello, world!</b>');
output.clear();
Return
HtmlOutput
— the HtmlOutput
itself, useful for chaining
getAs(contentType)
Return the data inside this object as a blob converted to the specified content type. This method adds the appropriate extension to the filename — for example, "myfile.pdf". However, it assumes that the part of the filename that follows the last period (if any) is an existing extension that should be replaced. Consequently, "ChristmasList.12.25.2014" will become "ChristmasList.12.25.pdf".
Parameters
Name | Type | Description |
---|---|---|
contentType | String | the MIME type to convert to. For most blobs,
'application/pdf' is the only valid option. For images in BMP, GIF, JPEG,
or PNG format, any of 'image/bmp' , 'image/gif' ,
'image/jpeg' , or 'image/png' are also valid. |
Return
Blob
— the data as a blob
getContent()
Gets the content of this HtmlOutput
.
// Log "<b>Hello, world!</b>"
var output = HtmlService.createHtmlOutput('<b>Hello, world!</b>');
Logger.log(output.getContent());
Return
String
— the content that will be served
getHeight()
Gets the initial height of the
custom dialog in Google
Docs, Sheets, or Forms. If the HtmlOutput
is published as a web app instead, this
method returns null
. To resize a dialog that is already open, call
google.script.host.setHeight(height)
in client-side code.
var output = HtmlService.createHtmlOutput('<b>Hello, world!</b>');
output.setHeight(200);
Logger.log(output.getHeight());
Return
Integer
— the height, in pixels
getTitle()
Gets the title of the output page. Note that the <title> HTML element is ignored.
var output = HtmlService.createHtmlOutput('<b>Hello, world!</b>');
Logger.log(output.getTitle());
Return
String
— the title of the page
getWidth()
Gets the initial width of the
custom dialog or sidebar
in Google Docs, Sheets, or Forms. If the HtmlOutput
is published as a web app instead,
this method returns null
. To resize a dialog that is already open, call
google.script.host.setWidth(width)
in client-side code. Sidebars that are already open cannot be resized.
var output = HtmlService.createHtmlOutput('<b>Hello, world!</b>');
output.setWidth(200);
Logger.log(output.getWidth());
Return
Integer
— the width in pixels
setContent(content)
Sets the content of this HtmlOutput
.
var output = HtmlService.createHtmlOutput();
output.setContent('<b>Hello, world!</b>');
Parameters
Name | Type | Description |
---|---|---|
content | String | the content to serve |
Return
HtmlOutput
— this HtmlOutput
itself, useful for chaining
setHeight(height)
Sets the initial height of the
custom dialog in Google
Docs, Sheets, or Forms. If the HtmlOutput
is published as a web app instead, this
method has no effect. To resize a dialog that is already open, call
google.script.host.setHeight(height)
in client-side code.
var output = HtmlService.createHtmlOutput('<b>Hello, world!</b>');
output.setHeight(200);
Parameters
Name | Type | Description |
---|---|---|
height | Integer | the new height in pixels; null results in a default value |
Return
HtmlOutput
— this HtmlOutput
itself, useful for chaining
setSandboxMode(mode)
Sets the sandbox mode
used for client-side scripts. To protect users
from being served malicious HTML or JavaScript, client-side code served from HTML service
executes in a security sandbox that imposes restrictions on the code. This method allows script
authors to choose between different versions of the sandbox. For more information, see the
guide to restrictions in HTML service.
If a script does not set a sandbox mode, Apps Script uses NATIVE
mode as the
default. Prior to February 2014, the default was EMULATED
. The default is subject to
change.
The IFRAME
mode imposes many fewer restrictions than the other sandbox modes and
runs fastest, but does not work at all in certain older browsers, including Internet Explorer
9. By contrast, EMULATED
mode is more likely to work in
older browsers that do not support ECMAScript 5 strict
mode, most notably Internet Explorer 9. NATIVE
mode is the middle ground. If
NATIVE
mode is set but not supported in the user's browser, the sandbox mode falls back
to EMULATED
mode for that user.
// Serve HTML with a defined sandbox mode (in Apps Script server-side code).
var output = HtmlService.createHtmlOutput('<b>Hello, world!</b>');
output.setSandboxMode(HtmlService.SandboxMode.IFRAME);
The sandbox mode can also be read in a client-side script by inspecting
google.script.sandbox.mode
. Note that this property returns the actual mode on the
client, which may differ from the mode requested on the server if the requested mode is not
supported in the user's browser.
<!-- Read the sandbox mode (in a client-side script). -->
<script>
alert(google.script.sandbox.mode);
</script>
Parameters
Name | Type | Description |
---|---|---|
mode | SandboxMode | the sandbox mode to use |
Return
HtmlOutput
— this HtmlOutput
itself, useful for chaining
setTitle(title)
Sets the title of the output page. For web apps, this will be the title of the entire page,
while for HtmlOutput
shown in Google Sheets, this will be the dialog title.
var output = HtmlService.createHtmlOutput('<b>Hello, world!</b>');
output.setTitle('My First Page');
Parameters
Name | Type | Description |
---|---|---|
title | String | the new title |
Return
HtmlOutput
— this HtmlOutput
itself, useful for chaining
setWidth(width)
Sets the initial width of a
custom dialog or sidebar
in Google Docs, Sheets, or Forms. If the HtmlOutput
is published as a web app instead,
this method has no effect. To resize a dialog that is already open, call
google.script.host.setWidth(width)
in client-side code. Sidebars that are already open cannot be resized.
var output = HtmlService.createHtmlOutput('<b>Hello, world!</b>');
output.setWidth(200);
Parameters
Name | Type | Description |
---|---|---|
width | Integer | the new width in pixels; null results in a default value |
Return
HtmlOutput
— this HtmlOutput
itself, useful for chaining