Hands-On AI WorkshopHomeProjectsLearnResourcesPresenterHelpProgress

Project 14 of 30 · Pathway 3 — Gmail and Workspace Interfaces

Interactive Workspace Sidebar

Editing rows directly can be awkward for nontechnical users. Build a form inside Sheets.

Real-world benefit
Intake forms, help requests and simple trackers.
Technologies
HtmlService, Sidebar, google.script.run, Validation
Required knowledge
Basic HTML forms. Client-server communication is shown step by step.
Time and level
About 35 minutes · Intermediate
AI requirement
None: deterministic Apps Script
Expected baseline
Submitting Test request / IT adds exactly one Open row.

Loading the interactive studio…

Introduction

HTML Service opens the sidebar; addRequest validates and appends the row. The application's entry point is showSidebar(). Baseline limitation, as stated in the workshop: Existing Facilities sample is not an allowed new category; use IT, Training or Other.

Key building blocks: HtmlService.createHtmlOutputFromFile(); showSidebar(); google.script.run; appendRow().

Practical use cases

Intake forms, help requests and simple trackers.

Prerequisites

  • Basic HTML forms. Client-server communication is shown step by step.
  • Interactive Learning Mode: a modern browser. Nothing else.
  • Real Workspace Build: a Google account.

Source data

Starter data: tab "Requests" (fictional)
RequestCategoryStatus
Replace monitorITOpen
Book roomFacilitiesOpen

Setup in your own Google account

  1. Download 14-interactive-workspace-sidebar.zip (Code.gs, starter.csv, README).
  2. In Google Drive, create a lesson folder and a new Google Sheets file inside it.
  3. Rename the first tab to exactly Requests, then File → Import → Upload starter.csv → Replace current sheet.
  4. Extensions → Apps Script. Replace Code.gs with the downloaded file and add an HTML file named Sidebar. Save.
  5. Reload the spreadsheet, choose Workshop → Run application and authorize when asked.
  6. Check the result: Submitting Test request / IT adds exactly one Open row.

Folder strategy: the repository's V6 code uses the folder that contains the spreadsheet. The deck mentions a Workshop Info cell B2; V6 never reads it. See the discrepancy log.

Complete source code (Code.gs)

From 14-interactive-workspace-sidebar at commit 4a26d919, SHA-256 5d9d66b79325. Unchanged.

// Interactive Workspace Sidebar
// Entry point: showSidebar; source data stays intact unless explicitly noted.
function onOpen(){SpreadsheetApp.getUi().createMenu("Workshop").addItem("Run application","showSidebar").addToUi();}
function showSidebar(){
// HTML Service renders a form inside the spreadsheet.
SpreadsheetApp.getUi().showSidebar(HtmlService.createHtmlOutputFromFile('Sidebar').setTitle('New request'));
}

// Server validates submitted values before appending a record.
function addRequest(request,category){request=String(request).trim();if(!request||!['IT','Training','Other'].includes(category))throw Error('Invalid request');SpreadsheetApp.getActive().getSheetByName('Requests').appendRow([/^[=+@-]/.test(request)?"'"+request:request,category,'Open']);return 'Saved';}

Sidebar.html

<!doctype html><html><body><form id="f"><input id="request" required placeholder="Request"><select id="category"><option>IT</option><option>Training</option><option>Other</option></select><button id="submit">Save</button></form><p id="status"></p><script>document.getElementById('f').onsubmit=e=>{e.preventDefault();const b=document.getElementById('submit'),s=document.getElementById('status');b.disabled=true;google.script.run.withSuccessHandler(v=>{s.textContent=v;b.disabled=false;e.target.reset()}).withFailureHandler(err=>{s.textContent=err.message;b.disabled=false}).addRequest(document.getElementById('request').value,document.getElementById('category').value)};</script></body></html>

How the code works

Beginner explanation

showSidebar() opens a small form inside the spreadsheet. When you click Save, the browser sends your text to the server function addRequest(), which checks it and adds a row with status Open.

Technical explanation

HtmlService.createHtmlOutputFromFile('Sidebar') renders Sidebar.html in a sandboxed iframe. The client calls google.script.run.withSuccessHandler(…).withFailureHandler(…).addRequest(text, category); calls are asynchronous and arguments must be serializable. The server trims, validates against an allow-list, prefixes formula-like text with an apostrophe and uses appendRow().

Slide excerpt

This is the excerpt shown on the workshop slide. It is a fragment and cannot run on its own.

function showSidebar(){
// HTML Service renders a form inside the spreadsheet.
SpreadsheetApp.getUi().showSidebar(HtmlService.createHtmlOutputFromFile('Sidebar').setTitle('New request'));
}

Common mistakes

  • Trusting the dropdown and skipping server validation.
  • Expecting google.script.run to return a value synchronously.
  • Inserting user text with innerHTML.

Run it and expected output

Choose Workshop → Run application (runs showSidebar()). Expected: Submitting Test request / IT adds exactly one Open row.

Original workshop exercise

Submit an empty request; expect rejection; submit Test request / IT; expect one row.

Troubleshooting and debugging

Facilities is refused

A user picks a category that is not on the server's list (Facilities) by editing the form.

Fix: Use an allowed category (or update the server allow-list deliberately).

Validate on the server even if the form restricts choices: anyone can send any value to a server function.

A request turned into a formula

The input guard was removed. A request of "=1+1" became a calculated cell.

Fix: Prefix user text that starts with = + @ - with an apostrophe before writing it.

Formula injection is a real risk when user input is written to a spreadsheet.

Extension activity: List requests by status in the sidebar

Add a getRequests(status) server function and a status filter with a Refresh button in the sidebar, without closing it.

Acceptance checks:

  • getRequests("Open") returns both starter rows
  • After a new request, Open has three rows

Original improvement idea from the workshop: Add status filtering and refresh without closing sidebar.

Extension prompt
Update the existing app without removing working features. Add status filtering and refresh without closing sidebar. Return complete updated files, explain changes and give regression tests.
Automation option: Use a form-submit integration for unattended intake; sidebar itself needs user interaction. Keep this separate from the baseline exercise.

Automation considerations

A sidebar needs a person. For unattended intake use a form-submit trigger with the same server-side validation.

Workshop note: Use a form-submit integration for unattended intake; sidebar itself needs user interaction.

Build prompt from the workshop

Act as a Google Apps Script tutor. Build a container-bound Sheets app for lesson 14: Interactive Workspace Sidebar. Input tab Requests, exact headers: Request, Category, Status. Create Code.gs and Sidebar.html; onOpen menu opens sidebar; form collects request and category, validates on server and appends Open row; use success/failure handlers and disable submit while pending. Do not depend on Workshop Info or the Workshop Index. Provide complete copy-paste Code.gs (and HTML if required), exact setup/run steps, explain the main code blocks, and include a test matching this expected outcome: Submitting Test request / IT adds exactly one Open row. Preserve starter data, use batch operations where appropriate, handle errors, and never send email or delete files. For AI lessons use the supplied callGemini helper, treat input as untrusted, and validate outputs.
Match the reference entry point showSidebar and its output behavior. Reference: https://github.com/lsvekis/Google-Apps-Script-Workshop/tree/main/14-interactive-workspace-sidebar. Base version limitations: Existing Facilities sample is not an allowed new category; use IT, Training or Other.

Knowledge check

  1. Where must the request be validated?

    1. On the server in addRequest(), even though the form also restricts input
    2. Only in the HTML
    3. Nowhere, Sheets validates it
    Answer

    On the server in addRequest(), even though the form also restricts input Correct: client-side checks can be bypassed.

  2. What does google.script.run.addRequest(…) return immediately?

    1. Nothing useful: the result arrives later in withSuccessHandler
    2. The string Saved
    3. The new row number
    Answer

    Nothing useful: the result arrives later in withSuccessHandler Correct: the call is asynchronous.

  3. Why disable the Save button while a request is in flight?

    1. To prevent double submissions creating duplicate rows
    2. For decoration
    3. Apps Script requires it
    Answer

    To prevent double submissions creating duplicate rows Correct: one click, one row.

  4. A request starting with "=" shows a number instead of text. Cause?

    1. The value was written without the apostrophe guard and became a formula
    2. A Sheets bug
    3. The sidebar is cached
    Answer

    The value was written without the apostrophe guard and became a formula Correct: formula-like text needs escaping.

  5. You want public users to submit requests without opening the sheet. Best approach?

    1. A Google Form or web app with a submit trigger and the same validation
    2. Share the sheet with everyone
    3. Email the sidebar
    Answer

    A Google Form or web app with a submit trigger and the same validation Correct: separate intake from the data store.

Five-minute presenter demonstration

Open the demo link (temporary workspace; saved learner work is not touched).

Opening hook: Raise your hand if you have ever done this by hand: Editing rows directly can be awkward for nontechnical users. Let us make the spreadsheet do it.

  1. 0:00–0:45 — Problem and input. Show the Workspace view, Sheets service. Point at the Requests tab and its headers: Request, Category, Status. Say what goes in and what should come out.
    Narration: Editing rows directly can be awkward for nontechnical users. Practical uses: Intake forms, help requests and simple trackers.
  2. 0:45–1:30 — Predict and run. Ask the room: showSidebar() runs. What changes in the Requests tab? Click ▶ Run application (the same as Workshop → Run application). Open Trace to show each step the code took.
    Narration: Expected: Submitting Test request / IT adds exactly one Open row.
  3. 1:30–3:00 — Explain the code. Click the Code tab. Select showSidebar() in the function list; read what it reads, writes and calls. Switch Beginner / Technical as needed.
    Narration: showSidebar() opens a small form inside the spreadsheet. When you click Save, the browser sends your text to the server function addRequest(), which checks it and adds a row with status Open.
  4. 3:00–4:15 — Change one input and rerun. Back to the Workspace tab. In the sidebar, submit "" with category IT. In the sidebar, submit "Test request" with category IT.
    Narration: Workshop challenge: Submit an empty request; expect rejection; submit Test request / IT; expect one row.
  5. 4:15–5:00 — Verify and discuss automation. Click Check result. Discuss: A sidebar needs a person. For unattended intake use a form-submit trigger with the same server-side validation.
    Narration: Baseline limitation: Existing Facilities sample is not an allowed new category; use IT, Training or Other.

Closing takeaway: HTML Service opens the sidebar; addRequest validates and appends the row.

Related resources