Setting up your local development environment for writing Google Apps Script projects using Typescript
Google Apps Script (GAS) is a superset of ES5, but does NOT include any ES6 features. Also, the browser-based GAS editor is not the best. To make GAS development less miserable, develop locally (i.e. not in the browser) using TypeScript instead!
Google's clasp tool will transpile your code from Typescript to GAS and upload to your GAS project where you can run it.
- Intall stuff!
- Visual Studio Code
npm- TypeScript:
npm install -g typescript clasp:npm install -g @google/clasp- Be sure to follow instructions for enabling the Apps Script API!
- ESLint:
npm install -g eslint - TSLint:
npm install -g tslint
- Create projects
- Create your GitHub repo
- Create a new Google Apps Script (GAS) project and save to a Team Drive
- From the GAS editor go to File > Project Properties and grab the Script ID
- Local setup
- Clone GitHub repo
- From terminal, navigate to repo directory and run
clasp clone <Script ID> - Run
npm init(OK to use all default settings - keep hitting enter) - Run
eslint --init(recommendation: use the Google style guide in json format) (also might need to usesudofor this and some of the following commands) - Run
tslint --init - Install type definitions for Apps Script in the project directory:
npm install --save @types/google-apps-script - Create a file named
jsconfig.jsonwith the following contents:{ "compilerOptions": { "lib": ["esnext"] } } - Create a file named
tsconfig.jsonwith the following contents:{ "compilerOptions": { "allowJs": true, "checkJs": true, "types": ["@types/google-apps-script"], "strictNullChecks": true } } - Recommended: disable some of the annoying
tslintrules. Opentslint.jsonand update the "rules" object to be:"rules": { "eofline": false, "interface-name": false, "object-literal-sort-keys": false, "no-consecutive-blank-lines": false },
- Create a file named
.claspignorewith the following contents to avoid pushing unneccesary files to the GAS project (note: it's a little tricky to get clasp to ignorenode_modules/):node_modules/* node_modules/** node_modules/**/.* node_modules/**/.*/** .eslintrc.js
- Build stuff!
- Simply rename any
.gsfiles to.tsand then go nuts with the TypeScript- e.g rename
Code.gstoCode.ts - Note: default parameter values apparently do NOT work after transpilation to GAS, so avoid them
- e.g rename
- OK to move .ts files to subdirectories; will still work just fine after transpilation/upload to GAS editor
- Use normal
git/GitHub process - Run
clasp push, which does two things:- Transpiles your
tscode togs(GAS code) - Uploads your code to the GAS project
- Transpiles your
- If there are any errors in your
tscode, theclasp pushmay look like it was successful, but actually fail. Look at the generatedgscode to be sure it was successful. You can force push withclasp push -fif need be. - Run
clasp opento open the GAS editor in your browser. From there you can finally execute your code. - Also from the GAS editor you can set up triggers as needed to automatically run your script periodically
- Simply rename any