Bringing A2UI to Google Workspace with Gemini
Source: Dev.to
An end‑to‑end example that uses Gemini’s structured output to render secure, dynamic, server‑driven UIs (e.g., booking forms, event lists) directly inside Google Sheets – no external infrastructure required.
📖 Overview
- A2UI – an emerging open‑standard protocol that lets AI agents generate rich, interactive user interfaces that render natively on web, mobile, and desktop.
- Secure‑by‑design – UI components are described with a strict schema‑based JSON format, eliminating the need for arbitrary, high‑risk code execution (e.g., XSS).
- Google Workspace integration – brings LLM‑driven agents from plain chat to actionable UI elements (forms, charts, data grids) inside the user’s workflow.
The full source code and documentation are hosted on GitHub:
🔗
🛠️ A2UI in Google Sheets – Lifecycle Diagram
flowchart TD
A[User Input (Dialog)] --> B[HTML captures text]
B --> C[google.script.run → GAS backend]
C --> D[Intent Routing (Gemini “Router”)]
D --> E[Select Sub‑application (e.g., Restaurant Finder)]
E --> F[Tool Execution (Sheets, Drive, Calendar …)]
F --> G[Send data back to Gemini]
G --> H[Gemini returns A2UI JSON + chat text]
H --> I[HTML renders UI components]
I --> J[Native UI shown in Sheet]
🚀 Implementation Details
| Step | Description |
|---|---|
| 1️⃣ User Input | In a Google Spreadsheet dialog, the user types a natural‑language request, e.g., “Find 3 Chinese restaurants in New York.” |
| 2️⃣ Request Transmission | The HTML UI sends the request to the Apps Script backend via google.script.run – a secure, direct bridge. |
| 3️⃣ Intent Routing | The backend forwards the request to the Gemini API. A specialized System Prompt makes Gemini act as a Router, selecting the appropriate sub‑application (Restaurant Finder, Event Manager, etc.). |
| 4️⃣ Tool Execution | The chosen sub‑application runs local GAS functions to query Sheets, fetch Drive files, or check Calendar availability. |
| 5️⃣ A2UI JSON Generation | Gemini receives the tool data and returns a structured response that contains:
|
| 6️⃣ Client‑Side Rendering | The backend returns the payload to the HTML dialog. A dedicated JavaScript renderer parses the A2UI JSON and builds UI components (cards, image lists, booking forms, etc.). |
| 7️⃣ Native Integration | Because the whole flow stays inside Google’s infrastructure (no external A2A server), latency is minimal and performance is high. |
Official A2UI Workflow (via A2A Protocol)
Frontend → A2A Server (Node.js/Python) → Gemini API → JSON → Frontend
Multiple network hops → higher latency.
GAS‑Native Workflow (via google.script.run)
Frontend (HTML) → google.script.run → GAS Backend → UrlFetchApp (Gemini) → JSON → Frontend
All communication occurs on Google’s internal high‑speed network → no external overhead.
✅ Why Choose the GAS‑Native Approach?
| Pros | Cons |
|---|---|
| Zero setup cost – No external servers; everything lives in a single Apps Script project. | Manual rendering logic – You must maintain the code that converts A2UI JSON into HTML elements (renderComponent). |
| Seamless Google integration – Directly read/write Sheets, Drive, Calendar, etc. | Execution quotas – 6‑minute script timeout, daily URL‑Fetch limits, etc., may restrict large‑scale use. |
| High performance – Eliminates extra network hops; UI appears instantly. |
📦 Deploying the Sample Application
-
Obtain a Gemini API Key
- Get a valid key from the Gemini console.
-
Copy the Sample Spreadsheet
- Use the “Make a copy” link.
-
Open the Apps Script editor
- In the copied spreadsheet:
Extensions → Apps Script.
- In the copied spreadsheet:
-
Add your Gemini API key
const GEMINI_API_KEY = 'YOUR_GEMINI_API_KEY'; -
Deploy as a Web App (optional)
Deploy → New deployment → Select type: Web app- Set Execute as → Me and Who has access → Anyone (or your preferred setting).
-
Run the demo
- Reload the spreadsheet, open the custom menu (or sidebar) that the script adds, and start typing natural‑language requests.
📚 Additional Resources
- A2UI Specification – https://github.com/agent2ui/spec
- Gemini API Documentation – https://cloud.google.com/vertex-ai/docs/generative-ai/overview
- Google Apps Script Guides – https://developers.google.com/apps-script
main.gs File
- Set your API key in the
apiKeyvariable. - Save the script.
Alternatively, you can visit the GitHub Repository to manually copy the source code.
- Once the script is set up, re‑open the Google Spreadsheet (or refresh the page).
- A table will appear on the “data” sheet; this serves as the source for the event demonstration.
- A custom menu labeled “sample” will appear in the toolbar.
- Select Sample → Run to open the A2UI dialog.
Demonstration Prompts
-
“Find 3 Chinese restaurants in New York”
- Replicates the official A2UI demonstration.
- Tests the system’s ability to retrieve mock data, render a list of restaurants with images, and present a booking form.
-
“Show me events for Jan 17‑20”
- Demonstrates a custom integration with Google Workspace.
- The agent searches the spreadsheet for events within the specified date range and presents them as a selectable list.
- Upon confirmation, the agent triggers a function to register the selected events directly to your Google Calendar.
Project Highlights
- Serverless Architecture – A2UI can be deployed using only Google Apps Script (GAS) and Google Sheets, eliminating the need for external web servers or hosting.
- Deep Workspace Integration – Seamlessly connects generative AI with Workspace tools, enabling actions like reading Sheets and writing to Calendar.
- Enhanced Security – By adhering to A2UI’s schema‑driven rendering, the application avoids the high risks associated with generating and executing arbitrary HTML/JS code.
- Dynamic User Experience – Users receive interactive, app‑like interfaces (forms, buttons, lists) directly within a chat dialog, vastly improving usability over plain text.
- Scalability to Other Apps – This approach is not limited to Sheets; it can be readily adapted for Google Docs, Slides, and other Workspace applications to create a unified AI interface.