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
`https://developers.google.com/apps-script/reference/html/html-output` for what limitations this implies on what can be returned.

### Properties

| Property | Type | Description |
|---|---|---|
| `SandboxMode` | `https://developers.google.com/apps-script/reference/html/sandbox-mode` | An enum representing the sandbox modes that can be used for client-side `HtmlService` scripts. |
| `XFrameOptionsMode` | `https://developers.google.com/apps-script/reference/html/x-frame-options-mode` | An enum representing the `X-Frame-Options` modes that can be used for client-side `HtmlService` scripts. |

### Methods

| Method | Return type | Brief description |
|---|---|---|
| `https://developers.google.com/apps-script/reference/html/html-service#createHtmlOutput()` | `https://developers.google.com/apps-script/reference/html/html-output` | Creates a new `https://developers.google.com/apps-script/reference/html/html-output` object that can be returned from the script. |
| `https://developers.google.com/apps-script/reference/html/html-service#createHtmlOutput(BlobSource)` | `https://developers.google.com/apps-script/reference/html/html-output` | Creates a new `https://developers.google.com/apps-script/reference/html/html-output` object from a `https://developers.google.com/apps-script/reference/base/blob-source.html` resource. |
| `https://developers.google.com/apps-script/reference/html/html-service#createHtmlOutput(String)` | `https://developers.google.com/apps-script/reference/html/html-output` | Creates a new `https://developers.google.com/apps-script/reference/html/html-output` object that can be returned from the script. |
| `https://developers.google.com/apps-script/reference/html/html-service#createHtmlOutputFromFile(String)` | `https://developers.google.com/apps-script/reference/html/html-output` | Creates a new `https://developers.google.com/apps-script/reference/html/html-output` object from a file in the code editor. |
| `https://developers.google.com/apps-script/reference/html/html-service#createTemplate(BlobSource)` | `https://developers.google.com/apps-script/reference/html/html-template` | Creates a new `https://developers.google.com/apps-script/reference/html/html-template` object from a `https://developers.google.com/apps-script/reference/base/blob-source.html` resource. |
| `https://developers.google.com/apps-script/reference/html/html-service#createTemplate(String)` | `https://developers.google.com/apps-script/reference/html/html-template` | Creates a new `https://developers.google.com/apps-script/reference/html/html-template` object that can be returned from the script. |
| `https://developers.google.com/apps-script/reference/html/html-service#createTemplateFromFile(String)` | `https://developers.google.com/apps-script/reference/html/html-template` | Creates a new `https://developers.google.com/apps-script/reference/html/html-template` object from a file in the code editor. |
| `https://developers.google.com/apps-script/reference/html/html-service#getUserAgent()` | `String` | Gets the user-agent string for the current browser. |

## Detailed documentation

### `createHtmlOutput()`

Creates a new `https://developers.google.com/apps-script/reference/html/html-output` object that can be returned from the script.

```javascript
const output = HtmlService.createHtmlOutput();
```

#### Return


`https://developers.google.com/apps-script/reference/html/html-output` --- the new HtmlOutput object

*** ** * ** ***

### `createHtmlOutput(blob)`

Creates a new `https://developers.google.com/apps-script/reference/html/html-output` object from a `https://developers.google.com/apps-script/reference/base/blob-source.html` resource.

```javascript
function createFromBlob(blob) {
  const output = HtmlService.createHtmlOutput(blob);
  return output;
}
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `blob` | `https://developers.google.com/apps-script/reference/base/blob-source.html` | the object to get HTML out of |

#### Return


`https://developers.google.com/apps-script/reference/html/html-output` --- the new `HtmlOutput` object

#### Throws


[`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) --- if the blob doesn't contain HTML or the HTML is malformed

*** ** * ** ***

### `createHtmlOutput(html)`

Creates a new `https://developers.google.com/apps-script/reference/html/html-output` object that can be returned from the script.

```javascript
const output = HtmlService.createHtmlOutput('<b>Hello world!</b>');
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `html` | `String` | the content to serve |

#### Return


`https://developers.google.com/apps-script/reference/html/html-output` --- the new HtmlOutput object

#### Throws


[`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) --- if the html is malformed

*** ** * ** ***

### `createHtmlOutputFromFile(filename)`

Creates a new `https://developers.google.com/apps-script/reference/html/html-output` object from a file in the code editor.

```javascript
const output = HtmlService.createHtmlOutputFromFile('myPage');
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `filename` | `String` | the name of the file to use |

#### Return


`https://developers.google.com/apps-script/reference/html/html-output` --- the new `HtmlOutput` object

#### Throws


[`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) --- if the file wasn't found or the HTML in it is malformed

*** ** * ** ***

### `createTemplate(blob)`

Creates a new `https://developers.google.com/apps-script/reference/html/html-template` object from a `https://developers.google.com/apps-script/reference/base/blob-source.html` resource.

```javascript
function createFromBlob(blob) {
  const template = HtmlService.createTemplate(blob);
  const output = template.evaluate();
  return output;
}
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `blob` | `https://developers.google.com/apps-script/reference/base/blob-source.html` | The object to get HTML out of. |

#### Return


`https://developers.google.com/apps-script/reference/html/html-template` --- the new `HtmlTemplate` object

#### Throws


[`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) --- if the blob doesn't contain HTML

*** ** * ** ***

### `createTemplate(html)`

Creates a new `https://developers.google.com/apps-script/reference/html/html-template` object that can be returned from the script.

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

#### Parameters

| Name | Type | Description |
|---|---|---|
| `html` | `String` | the content of the template |

#### Return


`https://developers.google.com/apps-script/reference/html/html-template` --- the new `HtmlTemplate` object

*** ** * ** ***

### `createTemplateFromFile(filename)`

Creates a new `https://developers.google.com/apps-script/reference/html/html-template` object from a file in the code editor.

```javascript
const template = HtmlService.createTemplateFromFile('myTemplate');
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `filename` | `String` | the name of the file to use |

#### Return


`https://developers.google.com/apps-script/reference/html/html-template` --- the new `HtmlTemplate` object

#### Throws


[`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/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