Advanced Google Apps Script Development

Abstract

This article describes techniques for offline editing and versioning of Google Apps Script (GAS) files located in standalone Apps Script projects.

Note: editing embedded script (in Sheets or Docs) will not work. A workaround is to use script libraries.

Objective

We want to solve the code versioning problem of GAS projects, i.e. the source code of earlier versions will not be available anymore after a newer version has been published. Keeping track of changes falls into the responsibility of the developer.

We furthermore would like to be able to edit source files with our preferred editor or IDE.

Setup

We will evaluate two techniques:

  1. A Node.js plugin for google apps script (gapps)
  2. The Google plugin for Eclipse as described here

Node

Follow this article for step by step setup.

Eclipse

Eclipse (neon) and Java Runtime Environment (JRE) was downloaded and installed and we followed this article

Note: The Google Plugin has to be installed in Eclipse as well.

Results

The Node.js plugin works perfectly to "check-out" a standalone script project to any local directory. After downloading the files, the project can be edited and versioned (e.g. with Tortoise/SVN). After local editing we can "check in" the latest version of the script files.

The Eclipse plugin worked perfectly. After importing a project, changes were made and stored back in Google Drive.

Conclusion

By now it seems a great opportunity to have version control over GAS code. Nonetheless, the development of HTML, CSS and JavaScript (at least for us) was already a local task (with Visual Studio). Only the communication with google objects (the real GAS) is a job that was done in the online editor from Google. A simple alternative (at least in order to have Google objects intellisense and autocomplete) is to work online and export files at the end of the day or to use a OneNote notebook with sections for the source code of every day or even every check in/major development step.

Eclise seems to be the better option due to intellisense and a more mature UI, e.g. saving a file is only one click away. With the Node.js tool, files have to be uploaded via command line interface. It is easy to import a project (picking a project via dialog), but in Eclipse as well as with gapps only stand-alone script files are supported, no embedded script in Sheets or Docs can be edited offline.

Snippets

gapps auth C:\Users\Admin\Downloads\client_secret.json

Your environment has been set up for using Node.js 4.2.3 (x64) and npm.

 

C:\Users\Admin>npm install -g node-google-apps-script

npm WARN deprecated tough-cookie@2.2.2: ReDoS vulnerability parsing Set-Cookie https://nodesecurity.io/advisories/130

C:\Users\Admin\AppData\Roaming\npm\gapps -> C:\Users\Admin\AppData\Roaming\npm\node_modules\node-google-apps-script\bin\gapps

node-google-apps-script@1.1.5 C:\Users\Admin\AppData\Roaming\npm\node_modules\node-google-apps-script

── colors@1.1.2

── bluebird@2.10.2

── commander@2.9.0 (graceful-readlink@1.0.1)

── mkdirp@0.5.1 (minimist@0.0.8)

── lodash@3.10.1

── node-dir@0.1.15 (minimatch@3.0.3)

── request@2.74.0 (tunnel-agent@0.4.3, aws-sign2@0.6.0, forever-agent@0.6.1, oauth-sign@0.8.2, is-typedarray@1.0.0, caseless@0.11.0, stringstream@0.0.5, aws4@1.4.1, isstream@0.1.2, json-stringify-safe@5.0.1, extend@3.0.0, tough-cookie@2.3.1, qs@6.2.1, node-uuid@1.4.7, form-data@1.0.0-rc4, combined-stream@1.0.5, mime-types@2.1.11, hawk@3.1.3, bl@1.1.2, http-signature@1.1.1, har-validator@2.0.6)

└── googleapis@2.1.7 (string-template@0.2.1, async@1.5.2, request@2.65.0, gapitoken@0.1.5, google-auth-library@0.9.8)

 

C:\Users\Admin>gapps

No command specified.

 

  Usage: gapps [options] [command]

 

 

  Commands:

 

    auth [options] <path/to/client/secret.json>  Authorize gapps to use the Google Drive API

    upload|push                                  Upload back to an Apps Script project in Google Drive. Run from root of project directory

    init|clone [options] <fileId>                Initialize project. The external Apps Script project must exist.

    oauth-callback-url                           Get the OAuth Callback URL for a project

 

  Options:

 

    -h, --help     output usage information

    -V, --version  output the version number

 

 

C:\Users\Admin>

C:\Users\Admin>gapps auth C:\Users\Admin\Downloads\client_secret.json

 

Please visit the following url in your browser (you'll only have to do this once): https://accounts.google.com/o/oauth2/auth?access_type=offline&approval_prompt=force&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.scripts&response_type=code&client_id=407615108645-nkvn9f0mt7jr7odtggiirvv2qigt39ut.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A2386

Successfully Authenticated with Google Drive!

 

C:\Users\Admin>gapps init 17zIGptUapVn2SXTdGkfBZd_EmFhuZVKmz-M7zF0XdC9NF0rwmplPSiiT

 

C:\Users\Admin>dir

 Volume in drive C has no label.

 Volume Serial Number is 4214-14B5

 

 Directory of C:\Users\Admin

 

12.08.2016  17:32    <DIR>          .

12.08.2016  17:32    <DIR>          ..

11.02.2016  16:03    <DIR>          .android

08.12.2015  19:10                27 .appcfg_nag

14.07.2016  10:29    <DIR>          .config

04.02.2016  21:41    <DIR>          .cordova

01.12.2015  12:32    <DIR>          .dnx

08.12.2015  22:00             1.341 .dockercfg

05.08.2016  13:53             3.865 .ganttproject

12.08.2016  17:29               255 .gapps

11.02.2016  15:59    <DIR>          .gradle

08.12.2015  20:52    <DIR>          .gsutil

12.08.2016  17:04                51 .node_repl_history

22.02.2016  15:04    <DIR>          .nuget

04.02.2016  21:59    <DIR>          .plugman

08.12.2015  20:49    <DIR>          .ssh

18.12.2015  23:00    <DIR>          .vs

01.12.2015  11:11    <DIR>          .vscode

11.08.2016  08:28    <DIR>          Contacts

11.08.2016  08:28    <DIR>          Desktop

11.08.2016  09:07    <DIR>          Documents

12.08.2016  17:27    <DIR>          Downloads

11.08.2016  08:28    <DIR>          Favorites

05.08.2016  13:53           977.101 ganttproject.log

12.08.2016  17:32                92 gapps.config.json

08.12.2015  19:24    <DIR>          Google

04.08.2016  14:07                 0 graph1.png

11.08.2016  08:28    <DIR>          Links

11.08.2016  08:28    <DIR>          Music

20.01.2016  21:30    <DIR>          ODBA

12.08.2016  09:59    <DIR>          OneDrive

11.08.2016  08:28    <DIR>          Pictures

11.08.2016  08:28    <DIR>          Saved Games

11.08.2016  08:28    <DIR>          Searches

12.08.2016  17:32    <DIR>          src

11.08.2016  08:28    <DIR>          Videos

               8 File(s)        982.732 bytes

              28 Dir(s)  241.791.131.648 bytes free

 

C:\Users\Admin>c:\temp

'c:\temp' is not recognized as an internal or external command,

operable program or batch file.

 

C:\Users\Admin>cd\

 

C:\>cd temp

 

C:\temp>md gas

 

C:\temp>cd gas

 

C:\temp\gas>dir

 Volume in drive C has no label.

 Volume Serial Number is 4214-14B5

 

 Directory of C:\temp\gas

 

12.08.2016  17:33    <DIR>          .

12.08.2016  17:33    <DIR>          ..

               0 File(s)              0 bytes

               2 Dir(s)  241.790.922.752 bytes free

 

C:\temp\gas>gapps init 17zIGptUapVn2SXTdGkfBZd_EmFhuZVKmz-M7zF0XdC9NF0rwmplPSiiT

 

C:\temp\gas>dir

 Volume in drive C has no label.

 Volume Serial Number is 4214-14B5

 

 Directory of C:\temp\gas

 

12.08.2016  17:34    <DIR>          .

12.08.2016  17:34    <DIR>          ..

12.08.2016  17:34                92 gapps.config.json

12.08.2016  17:34    <DIR>          src

               1 File(s)             92 bytes

               3 Dir(s)  241.790.922.752 bytes free

 

C:\temp\gas>gapps upload

Pushing back up to Google Drive...

The latest files were successfully uploaded to your Apps Script project.

 

C:\temp\gas>

Sources


Dieter Neumann