Get the FREE Ultimate OpenClaw Setup Guide →

google-apps-script

npx machina-cli add skill jezweb/claude-skills/google-apps-script --openclaw
Files (1)
SKILL.md
9.6 KB

Google Apps Script

Build automation scripts for Google Sheets and Workspace apps. Scripts run server-side on Google's infrastructure with a generous free tier.

What You Produce

  • Apps Script code pasted into Extensions > Apps Script
  • Custom menus, dialogs, sidebars
  • Automated triggers (on edit, time-driven, form submit)
  • Email notifications, PDF exports, API integrations

Workflow

Step 1: Understand the Automation

Ask what the user wants automated. Common scenarios:

  • Custom menu with actions (report generation, data processing)
  • Auto-triggered behaviour (on edit, on form submit, scheduled)
  • Sidebar app for data entry
  • Email notifications from sheet data
  • PDF export and distribution

Step 2: Generate the Script

Follow the structure template below. Every script needs a header comment, configuration constants at top, and onOpen() for menu setup.

Step 3: Provide Installation Instructions

All scripts install the same way:

  1. Open the Google Sheet
  2. Extensions > Apps Script
  3. Delete any existing code in the editor
  4. Paste the script
  5. Click Save
  6. Close the Apps Script tab
  7. Reload the spreadsheet (onOpen runs on page load)

Step 4: First-Time Authorisation

Each user gets a Google OAuth consent screen on first run. For unverified scripts (most internal scripts), users must click:

Advanced > Go to [Project Name] (unsafe) > Allow

This is a one-time step per user. Warn users about this in your output.


Script Structure Template

Every script should follow this pattern:

/**
 * [Project Name] - [Brief Description]
 *
 * [What it does, key features]
 *
 * INSTALL: Extensions > Apps Script > paste this > Save > Reload sheet
 */

// --- CONFIGURATION ---
const SOME_SETTING = 'value';

// --- MENU SETUP ---
function onOpen() {
  const ui = SpreadsheetApp.getUi();
  ui.createMenu('My Menu')
    .addItem('Do Something', 'myFunction')
    .addSeparator()
    .addSubMenu(ui.createMenu('More Options')
      .addItem('Option A', 'optionA'))
    .addToUi();
}

// --- FUNCTIONS ---
function myFunction() {
  // Implementation
}

Critical Rules

Public vs Private Functions

Functions ending with _ (underscore) are private and CANNOT be called from client-side HTML via google.script.run. This is a silent failure — the call simply doesn't work with no error.

// WRONG - dialog can't call this, fails silently
function doWork_() { return 'done'; }

// RIGHT - dialog can call this
function doWork() { return 'done'; }

Also applies to: Menu item function references must be public function names as strings.

Batch Operations (Critical for Performance)

Read/write data in bulk, never cell-by-cell. The difference is 70x.

// SLOW (70 seconds on 100x100) - reads one cell at a time
for (let i = 1; i <= 100; i++) {
  const val = sheet.getRange(i, 1).getValue();
}

// FAST (1 second) - reads all at once
const allData = sheet.getRange(1, 1, 100, 1).getValues();
for (const row of allData) {
  const val = row[0];
}

Always use getRange().getValues() / setValues() for bulk reads/writes.

V8 Runtime

V8 is the only runtime (Rhino was removed January 2026). Supports modern JavaScript: const, let, arrow functions, template literals, destructuring, classes, async/generators.

NOT available (use Apps Script alternatives):

Missing APIApps Script Alternative
setTimeout / setIntervalUtilities.sleep(ms) (blocking)
fetchUrlFetchApp.fetch()
FormDataBuild payload manually
URLString manipulation
cryptoUtilities.computeDigest() / Utilities.getUuid()

Flush Before Returning

Call SpreadsheetApp.flush() before returning from functions that modify the sheet, especially when called from HTML dialogs. Without it, changes may not be visible when the dialog shows "Done."

Simple vs Installable Triggers

FeatureSimple (onEdit)Installable
Auth requiredNoYes
Send emailNoYes
Access other filesNoYes
URL fetchNoYes
Open dialogsNoYes
Runs asActive userTrigger creator

Use simple triggers for lightweight reactions. Use installable triggers (via ScriptApp.newTrigger()) when you need email, external APIs, or cross-file access.

Custom Spreadsheet Functions

Functions used as =MY_FUNCTION() in cells have strict limitations:

/**
 * Calculates something custom.
 * @param {string} input The input value
 * @return {string} The result
 * @customfunction
 */
function MY_FUNCTION(input) {
  // Can use: basic JS, Utilities, CacheService
  // CANNOT use: MailApp, UrlFetchApp, SpreadsheetApp.getUi(), triggers
  return input.toUpperCase();
}
  • Must include @customfunction JSDoc tag
  • 30-second execution limit (vs 6 minutes for regular functions)
  • Cannot access services requiring authorisation

Modal Progress Dialog

Block user interaction during long operations with a spinner that auto-closes. This is the recommended pattern for any operation taking more than a few seconds.

Pattern: menu function > showProgress() > dialog calls action function > auto-close

function showProgress(message, serverFn) {
  const html = HtmlService.createHtmlOutput(`
    <!DOCTYPE html>
    <html>
    <head>
      <style>
        body {
          font-family: 'Google Sans', Arial, sans-serif;
          display: flex; flex-direction: column;
          align-items: center; justify-content: center;
          height: 100%; margin: 0; padding: 20px;
          box-sizing: border-box;
        }
        .spinner {
          width: 36px; height: 36px;
          border: 4px solid #e0e0e0;
          border-top: 4px solid #1a73e8;
          border-radius: 50%;
          animation: spin 0.8s linear infinite;
          margin-bottom: 16px;
        }
        @keyframes spin { to { transform: rotate(360deg); } }
        .message { font-size: 14px; color: #333; text-align: center; }
        .done { color: #1e8e3e; font-weight: 500; }
        .error { color: #d93025; font-weight: 500; }
      </style>
    </head>
    <body>
      <div class="spinner" id="spinner"></div>
      <div class="message" id="msg">${message}</div>
      <script>
        google.script.run
          .withSuccessHandler(function(result) {
            document.getElementById('spinner').style.display = 'none';
            var m = document.getElementById('msg');
            m.className = 'message done';
            m.innerText = 'Done! ' + (result || '');
            setTimeout(function() { google.script.host.close(); }, 1200);
          })
          .withFailureHandler(function(err) {
            document.getElementById('spinner').style.display = 'none';
            var m = document.getElementById('msg');
            m.className = 'message error';
            m.innerText = 'Error: ' + err.message;
            setTimeout(function() { google.script.host.close(); }, 3000);
          })
          .${serverFn}();
      </script>
    </body>
    </html>
  `).setWidth(320).setHeight(140);

  SpreadsheetApp.getUi().showModalDialog(html, 'Working...');
}

// Menu calls this wrapper
function menuDoWork() {
  showProgress('Processing data...', 'doTheWork');
}

// MUST be public (no underscore) for the dialog to call it
function doTheWork() {
  // ... do the work ...
  SpreadsheetApp.flush();
  return 'Processed 50 rows';  // shown in success message
}

Error Handling

Always wrap external calls in try/catch. Return meaningful messages to dialogs.

function fetchExternalData() {
  try {
    const response = UrlFetchApp.fetch('https://api.example.com/data', {
      headers: { 'Authorization': 'Bearer ' + getApiKey() },
      muteHttpExceptions: true
    });
    if (response.getResponseCode() !== 200) {
      throw new Error('API returned ' + response.getResponseCode());
    }
    return JSON.parse(response.getContentText());
  } catch (e) {
    Logger.log('Error: ' + e.message);
    throw e;  // re-throw for dialog error handler
  }
}

Error Prevention

MistakeFix
Dialog can't call functionRemove trailing _ from function name
Script is slow on large dataUse getValues()/setValues() batch operations
Changes not visible after dialogAdd SpreadsheetApp.flush() before return
onEdit can't send emailUse installable trigger via ScriptApp.newTrigger()
Custom function times out30s limit — simplify or move to regular function
setTimeout not foundUse Utilities.sleep(ms) (blocking)
Script exceeds 6 minBreak into chunks, use time-driven trigger for batches
Auth popup doesn't appearUser must click Advanced > Go to (unsafe) > Allow

Common Pattern Index

See references/patterns.md for complete code examples:

PatternWhen to Use
Custom menusAdding actions to the spreadsheet toolbar
Sidebar appsForms and data entry panels
TriggersAutomated reactions to edits, time, or form submissions
Email from sheetsSending reports, notifications, schedules
PDF exportGenerating and emailing sheet as PDF
Data validationCreating dropdowns from lists or ranges

See references/recipes.md for complete automation recipes (archive rows, highlight duplicates, auto-number, dashboards).

See references/quotas.md for execution limits, email quotas, and debugging tips.

Source

git clone https://github.com/jezweb/claude-skills/blob/main/plugins/integrations/skills/google-apps-script/SKILL.mdView on GitHub

Overview

Google Apps Script lets you automate Google Sheets and Workspace apps by running server-side code on Google's infrastructure. You can produce scripts with custom menus, dialogs, sidebars, and automated triggers, plus email notifications, PDF exports, and external API integrations. The workflow guides you from understanding the automation to installation and first-time authorization.

How This Skill Works

Create a script using a reusable template that includes a header comment, configuration constants, and an onOpen() function to build a custom menu. Paste the code into Extensions > Apps Script, then Save and reload to initialize the UI. Follow best practices like bulk data operations (getValues/setValues) and manage function visibility with underscores for private functions; public menu functions must be named as strings.

When to Use It

  • Need a custom menu with click actions (e.g., report generation or data processing).
  • Require auto-triggered behavior on edits, form submissions, or scheduled times.
  • Want a sidebar app for streamlined data entry and validation.
  • Need email notifications derived from sheet data.
  • Need to export dashboards or sheets to PDF and distribute them automatically.

Quick Start

  1. Step 1: Define what to automate (menu action, trigger, data flow).
  2. Step 2: Generate the script using the template, including header, config, and onOpen().
  3. Step 3: Install and authorize: open the Google Sheet, Extensions > Apps Script, paste, Save, Reload, and complete the OAuth prompt.

Best Practices

  • Structure every script with a header comment, configuration constants, and an onOpen() menu setup.
  • Read/write data in bulk using getRange().getValues() and setValues().
  • Use the V8 runtime and modern JavaScript features; avoid Rhino-era patterns.
  • Keep private functions distinct by suffixing with an underscore and avoid exposing them to client-side calls.
  • Document and warn users about the OAuth authorization step; provide clear one-time consent instructions.

Example Use Cases

  • Sales dashboard: a custom menu button runs data aggregation and emails a PDF report to stakeholders.
  • Project intake: a sidebar collects data and creates tasks automatically in a sheet-based tracker.
  • Form-driven updates: onFormSubmit triggers validate data and notify the team via email.
  • Threshold alerts: onEdit triggers alert emails when values exceed defined thresholds.
  • PDF distribution: scheduled scripts export dashboards as PDFs and share them with recipients.

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers