Note: The GitHub Workflows build will always be failing for this template
repo as it doesn't have a package-lock.json file. This is intentional! When
you run npm i
for your project, just make sure to commit the lockfile.
This repository provides a simple template to start a new Google Apps Script project with TypeScript. Building, linting, formatting, and pushing to Google Apps are all built in.
The project is set up with a TypeScript configuration that transforms your
TypeScript files in the src
directory into JavaScript files in the build
directory. It's not enough to upload those files as-is to Google Apps, however,
because Apps Script doesn't support installing and running dependencies, and
uploaded files are run in random order - they aren't linked together at all. To
solve this problem, a second build step uses Webpack to combine all files and
depdendencies into a single minified Code.js
file, which is the only file that
is actually pushed to Google Apps.
- Install Node.js.
- Click "Use This Template" to start a new repository. Clone your new repo and open a terminal in the local directory. This template includes some configuration for VSCode, but you can use any editor.
- Install all of the dependencies by running
npm install
. - Start a new Google Apps Script project by going to https://script.google.com/home and clicking New Project. You can also start a project tied directly to a specific Google Apps file by opening the file and clicking Tools -> Script Editor.
- Click File -> Project Properties in the Google Apps Script editor. Enter a project name if prompted to, then on the Info tab in the Project Properties window, find and copy the Script ID.
- Replace
SCRIPT_ID
in.clasp.json
with your project's ID. This ID is not a secret - you can safely commit it to your repository. - Add your TypeScript code in the
src
folder. Note that unlike in a normal Google Apps Script project, only what is imported into theindex.ts
file will be compiled into the script, and all files must be in thesrc
folder (except external dependencies). - Optional: lint your code with
npm run lint
and/or auto-format your code withnpm run format
. Typically you will want to install the ESLint and/or Prettier extensions in your preferred code editor. - Log in to Google Apps Script by running
npm run login
. Make sure to use the same account you used to make the Google Apps Script project. - Compile the TypeScript files into a single JavaScript file and push it to
Google Apps by running
npm run push
. Run this every time you make changes. If you ever change the project configuration (ie, by adding new APIs), you will need to sync theappsscript.json
file by runningnpm run pull
before you runnpm run push
.
You can run the following scripts by opening the project directory in your terminal (as long as you have Node & npm installed):
npm install
Install all required dependencies.npm run build
Build your project to ensure it compiles properly.npm run format
Format your code automatically using Prettier.npm run lint
Check your code for common errors using ESLint.npm run login
Login to the Google Apps Script client so you can push/pull. You will need to run this once on each device you use.npm run pull
Fetch the latest changes from Google Apps Script. You should only need to do this if you change the project settings.npm run push
Build your project and push it to Google Apps in one step. The first time you run this, you will be prompted to log in to your Google account.
The template comes with a built-in configuration file for
GitHub Workflows.
The workflow is configured to run on every push to the master
branch as well
as on every Pull Request as a Check, and it simply checks the code to ensure
that it passes linting and is buildable (has no type/syntax errors). This is
known as continuous integration and provides a form of automated testing to
ensure that your project's code is always valid.
For larger projects where more than one developer will be collaborating, it is
reccomended to protect the master
branch and only allow changes to it through
Pull Requests in which all checks pass. You can learn more about this from
GitHub's
help articles.
If you would like to disable the workflow, simply delete the
checks.yml
file.
The following notes have important information for writing code for Google Apps Script using this template. Please read through them before you start.
In order for your Google Apps to run any of your code, you'll need to expose one or more functions to the engine. In the traditional Google Apps Script environment, you'd do this by declaring global functions, however in this setup there is no concept of 'global' as all files are modules.
Instead, you need to add these to the global
object. This object type is declared
in src/global.d.ts
so you can assign to it from any file, although as a matter
of best-practice you may want to confine global assignments to your index.ts
file. Any function added to the global
object will be available under that
name to all
triggers and
anywhere else Google Apps might need to call your function, such as from a
custom menu.
Note that the field in global
that you assign the function, not the function's
name, is treated as the function name. For example, the following function will
be called by the onOpen
trigger, not the onEdit
trigger:
function onEdit() {}
global.onOpen = onEdit;
Further documentation is provided with the global
type definition in
global.d.ts
and examples for all the simple triggers are given in
index.ts
.
Circular dependencies (files that depend on each other in a circular manner) can
cause unexpected issues like "X is not a function" or "X is not defined". If you
are seeing these errors in your project and you know they are wrong, try
checking for circular dependencies using
madge
(not included in this template):
- Install
madge
globally withnpm i --global madge
. - Check for circular dependencies with
madge src/index.ts --circular
.
.github
workflows
All GitHub workflows are configured using YAML files in this directory.checks.yml
This is where the continuous integration workflow is configured.
src
This is where you'll put all your TypeScript files.example.ts
Gives an example of how to export something from a local file.index.ts
Provides you with the five basic triggers prebuilt, as well as an example of how to import from a local file.global.d.ts
Provides type-definitions for the global object from which triggers are exported.
.clasp.json
Provides the configuration for Clasp, the command-line tool which pushes code to Google Apps Script..claspignore
Tells Clasp to ignore every file except forCode.js
,appsscript.json
, andreadme_appsscript.md
..gitignore
Tells Git to ignore output files andnode_modules
..prettierrc
Configures Prettier, the code formatter.appsscript.json
Controls the project setup and Apps Script environment. You can change the exception logger, but don't change the runtime version.package.json
Lists the project dependencies and scripts.readme_appsscript.md
If anyone opens your project in the Apps Script editor, they'll just see a jumbled file. This note helps them realize that the project should be edited outside of that environment.readme.md
This file.tsconfig.json
Configures the TypeScript compiler to enable strict mode and compile to CommonJS modules from thesrc
folder to thebuild
folder.webpack.config.js
Configures Webpack to take files from thebuild
folder and dependencies and merge them all into a singleCode.js
file.