Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
Public template

About

Setup a local development environment inside Visual Studio Code and build Google Workspace add-ons with Google Apps Script

Topics

Resources

Stars

1.3k stars

Watchers

36 watching

Forks

Repository files navigation

Google Apps Script Starter Kit

Google Apps Script MIT License PRs Welcome

Write Google Apps Script in VS Code with modern JavaScript, ES modules, tests and linting. Vite bundles your code into one file, and clasp uploads it to your Apps Script project. Digital Inspiration uses this kit to build Mail Merge, Form Notifications and Document Studio.

Google Apps Script Development with ES6

Quick start

You need Node.js 20.19 or later, and the Apps Script API turned on in your Apps Script settings. clasp cannot create or push projects without it.

npx degit labnol/apps-script-starter my-project
cd my-project
npm install
npx clasp login
npx clasp create --type sheets --title "My Project" --rootDir dist
npm run deploy
npx clasp open-script

clasp create makes a new Google Sheet with a bound script and writes .clasp.json. Use --type standalone for a script without a file, or npx clasp clone <scriptId> --rootDir dist to use a project you already have.

How it works

Apps Script can only call top-level global functions. Vite bundles src/ into dist/code.js, and a small plugin (vite-plugins/gas-expose.js) adds a global function for each export of src/index.js. If a function is not exported there, Apps Script cannot call it.

// src/index.js
import { getGmailAliases } from './server/gmail.js';
import { doGet } from './server/webapp.js';

// doGet serves the web app. getGmailAliases can be called from the HTML with google.script.run.
// Add triggers such as onOpen or onEdit to this list the same way.
export { doGet, getGmailAliases };

Some files are copied into dist/ as they are, without bundling:

  • functions/*.js: custom functions for Google Sheets, such as =DOUBLE(A1). Sheets needs them as plain global functions with a JSDoc @customfunction tag.
  • src/html/*.html: HTML for web apps, sidebars and dialogs. Load them by file name, for example HtmlService.createHtmlOutputFromFile('index').
  • appsscript.json: the project manifest. It also loads Apps Script libraries such as OAuth2.

Commands

Command What it does
npm run dev Rebuilds dist/ when a file changes. It does not upload.
npm run deploy Builds and pushes dist/ to Apps Script.
npm test Runs the Node.js tests (*.test.js, next to the code they test).
npm run lint Checks the code with ESLint.
npm run format Formats the code with Prettier.
npx clasp open-script Opens the project in the Apps Script editor.
npx clasp open-logs Opens the execution logs.
npx clasp deploy Creates a versioned deployment, for example to publish a web app.

To upload on every save, run npm run dev in one terminal and npx clasp push --watch in another.

Tests run in Node, where Apps Script services such as SpreadsheetApp do not exist. Keep the logic in plain functions you can test, as http.test.js does, or mock the services.

Project layout

src/index.js         Exports that become global functions
src/server/          Server-side code
src/html/            HTML files, copied as-is
functions/           Sheets custom functions, copied as-is
appsscript.json      Manifest: scopes, services, libraries, web app settings
vite.config.js       Build settings (output folder, minify, copied files)
eslint.config.js     Lint rules from @labnol/js-config, plus local overrides

Edit the manifest first

The sample appsscript.json is an example. Change it before you deploy:

  • oauthScopes: list every scope your code needs. When this field is set, Apps Script grants only these scopes. See scopes.md for the full list.
  • timeZone: set your own time zone. The sample uses Asia/Calcutta.
  • webapp: the sample lets anyone open the web app, and it runs as the user who opens it. Change access if you don't want that.
  • enabledAdvancedServices and libraries: remove the ones you don't use. The sample turns on the advanced Gmail and Drive services, and adds the OAuth1 and OAuth2 libraries.

Linting and formatting

ESLint and Prettier settings come from @labnol/js-config. Its Apps Script preset already knows globals such as SpreadsheetApp and google.script.run. Install the VS Code extension pack to see lint errors and format on save.

License

MIT. See LICENSE. Built by Amit Agarwal at Digital Inspiration. The video tutorial is from 2019 and shows an older webpack version of this kit.

About

Setup a local development environment inside Visual Studio Code and build Google Workspace add-ons with Google Apps Script

Topics

Resources

Stars

1.3k stars

Watchers

36 watching

Forks

Used by

Contributors

Languages