Google Apps Script
Feedback on this document

Class HtmlOutput

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 JavaScript that manipulates the DOM, not Apps Script). All of this content is sanitized using Google Caja, which implies some limitations as to what JavaScript is allowed. Specifically, any JavaScript content must conform to the ES5-strict standard, which permits most standard JavaScript, but not all. An exception is the "eval" statement, which is allowed in ES5-strict, but which will only be supported by Apps Script in some browsers. In general, it is best not to rely on "eval" being available.

See also

Methods

MethodReturn typeBrief description
append(addedContent)HtmlOutputAppends new content to the content of this HtmlOutput.
appendUntrusted(addedContent)HtmlOutputAppends new content to the content of this HtmlOutput, using contextual escaping.
asTemplate()HtmlTemplateReturns an HtmlTemplate backed by this HtmlOutput.
clear()HtmlOutputClears the current content.
getAs(contentType)CompositeBlobReturn the data inside this object as a Blob converted to the specified content type.
getBlob()BlobReturn the data inside this object as a Blob.
getContent()StringGets the content of this HtmlOutput.
getHeight()IntegerGets the height of the output dialog.
getTitle()StringGets the title of the output page.
getWidth()IntegerGets the width of the output dialog.
setContent(content)HtmlOutputSets the content of this HtmlOutput.
setHeight(height)HtmlOutputSets the height of the output dialog.
setTitle(title)HtmlOutputSets the title of the output page.
setWidth(width)HtmlOutputSets the width of the output dialog.

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.

 // This code will 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

NameTypeDescription
addedContentStringthe 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.

 // This code will log "<b>Hello world</b>&lt;p&gt;Hello again world&lt;/p&gt;"
 var output = HtmlService.createHtmlOutput('<b>Hello world</b>');
 output.appendUntrusted('<p>Hello again world</p>');
 Logger.log(output.getContent());
 

Parameters

NameTypeDescription
addedContentStringthe 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.

Parameters

NameTypeDescription
contentTypeStringthe 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

CompositeBlob — the data as a blob


getBlob()

Return the data inside this object as a Blob.

Return

Blob — the data as a blob


getContent()

Gets the content of this HtmlOutput.

 // The following code will 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 height of the output dialog.

This is only relevant when displaying the HtmlOutput inside a Spreadsheet.

 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 width of the output dialog.

This is only relevant when displaying the HtmlOutput inside a Spreadsheet.

 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

NameTypeDescription
contentStringthe content to serve

Return

HtmlOutput — this HtmlOutput itself, useful for chaining


setHeight(height)

Sets the height of the output dialog.

This is only relevant when displaying the HtmlOutput inside a Spreadsheet. It will be ignored when an HtmlOutput is published as a web app.

 var output = HtmlService.createHtmlOutput('<b>Hello world</b>');
 output.setHeight(200);
 

Parameters

NameTypeDescription
heightIntegerthe new height, in pixels. If you set height to null, it will use a default value.

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 an HtmlOutput shown in a Spreadsheet this will be the dialog title.

 var output = HtmlService.createHtmlOutput('<b>Hello world</b>');
 output.setTitle('My First Page');
 

Parameters

NameTypeDescription
titleStringthe new title

Return

HtmlOutput — this HtmlOutput itself, useful for chaining


setWidth(width)

Sets the width of the output dialog.

This is only relevant when displaying the HtmlOutput inside a Spreadsheet. It will be ignored when an HtmlOutput is published as a web app.

 var output = HtmlService.createHtmlOutput('<b>Hello world</b>');
 output.setWidth(200);
 

Parameters

NameTypeDescription
widthIntegerthe new width, in pixels. If you set width to null, it will use a default value.

Return

HtmlOutput — this HtmlOutput itself, useful for chaining

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.