/Google-Workspace-Addon-Boilerplate

A boilerplate for Google Workspace add-ons using nodejs, ES6, Typescript and Firebase for the backend.

Primary LanguageTypeScript

Your boilerplate project for
developing Google Workspace add-ons

***

Google Workspace add-ons are the extensions that can be found in the side panel of most Google apps (Gmail, Google Drive, Sheets, Docs, Slides, etc), unlike the more complexe Editor add-ons that can be found in the "Add-ons" tab of Google Sheets, Docs and Slides.


📝 Table of Contents


🔎 About

Google Apps Script is Google's Javascript-based development platform for building applications and add-ons for Google Sheets, Docs, Forms and other Google Apps.

This repo is a boilerplate project that uses the same development tools that you use for building traditional web apps (ES6, TypeScript, Node, Firebase, etc), all inside Google Apps Script projects.

See below how to get started!


🚜 Install

These instructions will get you set up with a copy of the project code on your local machine. It will also get you logged in to clasp so you can manage script projects from the command line.

See deploy for notes on how to deploy the project and see it live in Gmail, Calendar, and Drive.

Prerequisites

🏁 Getting started

1. First, let's clone the repo and install the dependencies.

git clone https://github.com/nathgilson/Google-Workspace-Addon-Boilerplate
cd Google-Workspace-Addon-Boilerplate
cd addOn
npm install

2. Next, we'll need to log in to clasp, which lets us manage our Google Apps Script projects locally.

npm run login

3. Now let's run the setup script to create a New spreadsheet and script project from the command line.

npm run setup

Alternatively, you can use an existing Google Spreadsheet and Script file instead of creating a new one.

See instructions here for using an existing project.

You will need to update the .clasp.json file in the root of this project with the following three key/value pairs:

{
  "scriptId": "1PY037hPcy................................................",
  "parentId": ["1Df30......................................."],
  "rootDir": "./dist"
}
  • scriptId: Your existing script project's scriptId. You can find it by opening your spreadsheet, selecting Tools > Script Editor from the menubar, then File > Project properties, and it will be listed as "Script ID".

  • parentId (optional): An array with a single string, the ID of the parent file (spreadsheet, doc, etc.) that the script project is bound to. You can get this ID from the url, where the format is usually https://docs.google.com/spreadsheets/d/{id}/edit. This allows you to run npm run open and open your file directly from the command line.

  • rootDir: This should always be "./dist", i.e. the local build folder that is used to store project files.

Next, let's deploy the app so we can see it live in Google Workspace.


🚀 Deploy

Run the deploy command. You may be prompted to update your manifest file. Type 'yes'.

npm run deploy

The deploy command will build all necessary files using production settings, including all server code (Google Apps Script code) and config files. All bundled files will be outputted to the dist/ folder, then pushed to the Google Apps Script project.

Now open Google Sheets and navigate to your new spreadsheet (e.g. the file "My Project"). You can also run npm run open. Make sure to refresh the page if you already had it open. You will now see a new menu item appear containing your app!


⛏️ Usage

The included sample app

The sample app is inspired from Google's quickstart: Cats Google Workspace Add-on

Typescript

This project supports typescript.

To use, simply use a typescript extension in the code (.ts), and your typescript file will compile to the proper format.

To use typescript in server code, just change the file extension to .ts. The server-side code already utilizes type definitions for Google Apps Script APIs.

A basic typescript configuration is used here, because after code is transpiled from typescript to javascript it is once again transpiled to code that is compatible with Google Apps Script. However, if you want more control over your setup you can modify the included tsconfig.json file.

Adding packages

For instance, install axios from npm:

npm install axios

Important: Since Google Apps Scripts projects don't let you easily reference external files, this project will bundle an entire app into one .gs file. This can result in large files if you are importing large packages. To help split up the files, you can grab a CDN url for your package and declare it in the webpack file, here. If set up properly, this will add a script tag that will load packages from a CDN, reducing your bundle size.

Modifying scopes

If you modify this project to work with Google Forms or Calendar, make sure to edit the oauthScopes in the appscript.json file.

See https://developers.google.com/apps-script/manifest for information on the appsscript.json structure.

Autocomplete

This project includes support for autocompletion and complete type definitions for Google Apps Script methods.

All available methods from the Google Apps Script API are shown with full definitions and links to the official documentation, plus information on argument, return type and sample code.

Card builder

The online Card builder can help you prototype your app's interface.