Google Apps Script Client

1.2.1 · active · verified Wed Apr 22

gas-client is a client-side utility library designed for Google Apps Script projects, providing a modern, promise-based interface for calling server-side Apps Script functions. It acts as a user-friendly wrapper around the native `google.script.run` API, abstracting away callback-based error and success handling in favor of standard JavaScript Promises and async/await syntax. The current stable version is 1.2.1, with minor releases and patches occurring as needed to address compatibility and improvements. A key differentiator is its ability to provide consistent access to `google.script.host` functions (like `close()` or `setHeight()`) in both production Apps Script environments and local development setups. It is specifically designed to integrate seamlessly with local development servers, such as those used in React Google Apps Script projects, by allowing explicit configuration of allowed development domains. This enables a more streamlined and conventional client-side development workflow for Apps Script.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to initialize `GASClient`, call a server-side Apps Script function using promises or async/await, optionally configure it for local development, and utilize `scriptHostFunctions`.

import { GASClient } from 'gas-client';

// Initialize GASClient, optionally for local development
const clientConfig = {
  // In development mode, replace 3000 with your local dev server port
  // This setting is ignored in production Apps Script environments.
  allowedDevelopmentDomains: process.env.NODE_ENV === 'development' ? 'https://localhost:3000' : undefined,
};
const { serverFunctions, scriptHostFunctions } = new GASClient(clientConfig);

// Example 1: Calling a server function with Promises
const sheetTitle = 'MyNewSheet';
serverFunctions.addSheet(sheetTitle)
  .then((response) => {
    console.log('Sheet added successfully:', response);
    scriptHostFunctions.setHeight(500); // Example of using a host function
  })
  .catch((err) => {
    console.error('Failed to add sheet:', err);
  });

// Example 2: Calling a server function with async/await
async function createAndLogSheet(title: string) {
  try {
    const response = await serverFunctions.addSheet(title);
    console.log('Sheet created via async/await:', response);
    scriptHostFunctions.focusEditor(); // Switch focus to the editor
  } catch (err) {
    console.error('Error in async/await:', err);
  }
}

// Call the async function
createAndLogSheet('AnotherSheet');

view raw JSON →