What is HtmlService in Google Sheets?

HtmlService lets Apps Script show custom HTML as a dialog, sidebar, or full web app page inside or beside your spreadsheet. You design the UI with HTML and CSS, then call server functions with google.script.run to read and write sheet data without building a separate website.

When to use it

Use HtmlService when you need a friendlier interface than a raw grid: multi-step forms, search boxes, approval buttons, or dashboards that pull live data from the active sheet.

When to skip it

Skip HtmlService for tiny tasks a Browser.msgBox or custom menu item can handle. Full web apps with heavy traffic may need tighter security review than a simple sidebar.

How it works

  1. 1

    Create an HTML file in the Apps Script project, for example Sidebar.html.

  2. 2

    In Code.gs, return HtmlService.createHtmlOutputFromFile('Sidebar') or createTemplateFromFile for dynamic placeholders.

  3. 3

    Show the UI with SpreadsheetApp.getUi().showSidebar(html) or showModalDialog(html, 'Title').

  4. 4

    In the HTML, use google.script.run.withSuccessHandler(fn).serverFunction(args) to call functions in Code.gs.

  5. 5

    Deploy as a web app when the page should open from a URL instead of inside Sheets.

  6. 6

    Test permissions because server functions run with the same scopes as the rest of your script.

Examples in Google Sheets

Search sidebar

A sidebar input calls findRows(query) on the server, which returns matching rows as JSON and renders them in a table in the sidebar.

Approval modal

A custom menu opens a modal dialog with Approve and Reject buttons that call updateRowStatus(rowId, status) on the active row.

Web app form

HtmlService serves a public form page that posts new entries to the sheet through a server function, without sharing the whole workbook.

Build this without starting from a blank cell

Use a Better Sheets tool for HtmlService, then watch a walkthrough when you want the full pattern.

Better Sheets resources

Common mistakes

  • Putting API secrets in client-side script tags where users can view source.
  • Not using withFailureHandler on google.script.run, so errors fail silently in the UI.
  • Loading huge datasets into the browser at once instead of paging or filtering on the server.
  • Forgetting XFrameOptionsMode when embedding breaks in iframes on external sites.
  • Deploying a web app with Anyone access while server functions return private sheet data.

Frequently asked questions

What is the difference between a sidebar and a dialog?
A sidebar docks beside the sheet and stays open while users edit. A modal dialog blocks interaction until they close it.
Can HtmlService use external CSS or JavaScript libraries?
You can link CDN assets in HTML, but sandbox and CSP rules may limit some resources. Test in your target environment.
How does google.script.run work?
It calls a function in Code.gs asynchronously. Use success and failure handlers to update the page when the server responds.
Can I use HtmlService without opening the sheet?
As a deployed web app, yes. Users visit the deployment URL and server code still accesses the bound spreadsheet per deployment settings.
Why is my sidebar blank?
Check the HTML filename matches createHtmlOutputFromFile, open Executions for server errors, and look for JavaScript errors in the browser console.
Does HtmlService work on mobile Sheets?
Sidebars and dialogs are limited on mobile. Web app URLs in a mobile browser often work better for field use.
Can HtmlService return JSON only?
Web apps can return JSON from doGet without HTML, but HtmlService itself is for HTML UIs. ContentService handles plain text and JSON responses.
How do I pass data from the sheet into HTML?
Use a template file with <?= variable ?> placeholders, or call a server function on load that returns the data you need.

Related Tutorials

Watch how HtmlService works

Browse more tutorials
Convert Google Sheets into a REST API

Convert Google Sheets into a REST API

How to use Google Sheets as a database by turning it into a REST API wit...
Sheet Styles A Free Google Sheet Add-on by Better Sheets

Sheet Styles A Free Google Sheet Add-on by Better Sheets

Install Sheet Styles now: https://sheet-styles.bettersheets.co/
Embed a Headline in a Website from Google Sheets

Embed a Headline in a Website from Google Sheets

One of the most requested videos, Ever. How to put data, a number, some ...
Create Navigation Buttons in Sheets to Other Sheets

Create Navigation Buttons in Sheets to Other Sheets

Add a button to go to another sheet inside your sheet.
Ali Abdaal's Google Sheet | Year at a Glance

Ali Abdaal's Google Sheet | Year at a Glance

Ali gave away a free Google Sheet Template. In this tutorial I step thro...
Add Click Tracking To Your Google Sheets | Bitly in a Google Sheet

Add Click Tracking To Your Google Sheets | Bitly in a Google Sheet

Create a click Edit: Google changed how the redirect is handled. It no l...

Related blog posts

Guides that explain HtmlService in more depth.

Browse the blog

Related glossary terms

Done reading about HtmlService?

Membership unlocks 636+ tutorials, unlimited generators, and every template. Practical lessons. Zero fluff.

Need this once

Jump to a free tool or a single tutorial for this topic.

Learning Sheets for real

Unlock the full library, generators, and templates with membership.