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

Properties

PropertyTypeDescription
SandboxModeSandboxModeAn enum representing the sandbox modes that can be used for client-side HtmlService scripts.
XFrameOptionsModeXFrameOptionsModeAn enum representing the X-Frame-Options modes that can be used for client-side HtmlService scripts.

Methods

MethodReturn typeBrief description
createHtmlOutput()HtmlOutputCreates a new HtmlOutput object that can be returned from the script.
createHtmlOutput(blob)HtmlOutputCreates a new HtmlOutput object from a BlobSource resource.
createHtmlOutput(html)HtmlOutputCreates a new HtmlOutput object that can be returned from the script.
createHtmlOutputFromFile(filename)HtmlOutputCreates a new HtmlOutput object from a file in the code editor.
createTemplate(blob)HtmlTemplateCreates a new HtmlTemplate object from a BlobSource resource.
createTemplate(html)HtmlTemplateCreates a new HtmlTemplate object that can be returned from the script.
createTemplateFromFile(filename)HtmlTemplateCreates a new HtmlTemplate object from a file in the code editor.
getUserAgent()StringGets 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

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

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

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

NameTypeDescription
blobBlobSourceThe 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 &lt;?= new Date() ?&gt;</b>',
);

Parameters

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

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