Google Apps Script Client
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
-
TypeError: Cannot read properties of undefined (reading 'someServerFunction')
cause Attempting to call a server function directly on the `GASClient` instance or without correctly destructuring `serverFunctions`.fixEnsure `serverFunctions` is properly destructured from a `new GASClient()` instance: `const { serverFunctions } = new GASClient(config); serverFunctions.someServerFunction();` -
Refused to connect to 'https://localhost:3000/' because it violates the following Content Security Policy directive...
cause The `allowedDevelopmentDomains` configuration is either missing, incorrect, or doesn't match the actual URL of your local development server, preventing communication between the client and the mock server.fixSet `allowedDevelopmentDomains` in your `GASClient` constructor options to the exact `https://localhost:PORT` URL of your running development server (e.g., `{ allowedDevelopmentDomains: 'https://localhost:3000' }`). -
Unhandled promise rejection
cause Server functions called via `serverFunctions` return Promises, and if an error occurs on the server-side Apps Script or during the call, the Promise will reject without being caught.fixAlways attach a `.catch()` handler to your promise chain (e.g., `serverFunctions.myFunc().then(...).catch(err => console.error(err))`) or wrap `await` calls in `try...catch` blocks (`try { await serverFunctions.myFunc(); } catch (err) { console.error(err); }`) to handle potential errors.
Warnings
- breaking The `v1.0.0` release introduced full TypeScript support and significant internal improvements, moving from a `v0.x` pre-release status to a stable major version. While specific breaking changes are not detailed, upgrading from `0.x` to `1.0.0` or higher may require code adjustments, particularly around type definitions and module resolution if you were using custom workarounds for types.
- gotcha When developing locally, the `allowedDevelopmentDomains` configuration option is crucial. If omitted or incorrectly specified, client-side communication with your local development server (e.g., via `Google Apps Script Webpack Dev Server`) will fail, preventing server function calls.
- gotcha Prior to `v1.2.1`, attempts to use `scriptHostFunctions.focusEditor()` within a Google Apps Script web app (as opposed to dialogs or sidebars) could potentially lead to errors if `google.script.host.editor` was not fully available in that context, causing `gas-client` to fail loading.
- gotcha `gas-client` wraps the native `google.script.run` with Promises. Developers accustomed to `google.script.run.withSuccessHandler().withFailureHandler()` must transition to standard Promise handling (`.then().catch()`) or `async/await` (`try...catch`) for all server function calls made via `serverFunctions`.
Install
-
npm install gas-client -
yarn add gas-client -
pnpm add gas-client
Imports
- GASClient
const GASClient = require('gas-client');import { GASClient } from 'gas-client'; - serverFunctions
import { serverFunctions } from 'gas-client';const { serverFunctions } = new GASClient(); - scriptHostFunctions
import { scriptHostFunctions } from 'gas-client';const { scriptHostFunctions } = new GASClient();
Quickstart
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');