Using ES6 and npm modules in Google Apps Script

Google’s Apps Script should be of interest to all JavaScript developers.
Apps Script is good for automation. Through it, you can directly access Google’s many services, such as Google Sheets, Gmail, Google Docs, Google Calendar, and more.
With a little bit of imagination, you can create very exciting Apps and add-ons using Google Apps Script.

Unlike AppEngine, which first asks you for your credit card, Apps Script remains free so far. At the very least, Apps Script is good for quickly creating “proof of concept” mockups or even prototypes.

There are different use cases for Apps Script. Primarily, it can be used to create add-ons for Google Sheets, Docs, or Forms. However, it can also create “standalone” web apps.

We will explore the standalone web app option in this article.

Although there is much to be excited about with Apps Script, it nonetheless has some very painful limitations

  1. Apps Script supports a very old version of JavaScript (JavaScript 1.6). Many of the modern JavaScript features that you may want to use are thus not available in Apps Script.
  2. There is no direct way to use npm modules (it is still possible, though, and I will show you how).
  3. It is quite difficult to create a good UI (with bootstrap, Vue, or even custom CSS). We have to find ways to inline custom script into HTML pages.
  4. You get a long, ugly URL as the address of your web app. It is difficult to share, and forget hosting a commercial offering on such an address.
  5. The “Apps Script” is rather painful. The correct name, by the way, is indeed “Apps,” followed by a space, then “Script.” There couldn’t be a less imaginative name for the thing. Some may like the name, but I’ve yet to meet anyone who claims to love it! Your hate will intensify when you start googling for an Apps Script function reference or example. There is a somewhat popular abbreviation: GAS (Google Apps Script). However, I really doubt whether even Google itself will be able to figure out what you mean if you search “use GAS in sheets.”

This article series is my attempt to circumvent Apps Script’s limitations and add some cool awesomeness to “standalone” web apps and add-ons.

First, we will use webpack and babel and create a bundle from ES6 Javascript code. In the next step, we will use npm packages in our Apps Script project. And in the next part of the series, we will develop modern UIs that make use of CSS frameworks and VueJS or ReactJS in your Apps Script project. Let’s dive into it!

Setting up your local Apps Script Environment

First, you have to get familiar with the Apps Script environment. Google provides a command line tool called clasp to manage Apps Script Projects locally.

Install clasp command line tool:

Once installed, login to your Google account.

This will open an authorisation page in your browser. You have to complete the steps.

Once these steps are done, you are ready to create your first Apps Script Project.

A simple Apps Script “stand alone” web app

Create a new folder. Open terminal and go to the new folder. Run the following command to create a new Apps Script project:

Create a new file app.js in the same folder. Add the following function in app.js file:

For a webapp type Appscript project you need to have a function named doGet(). doGet() is the function that does the rendering of the webpage.
In the example above, the output is a simple text. A common webapp should return a complete HTML page. We will go with the simple text for keeping this first project as simple as possible.

Open appscript.json. This file contains the settings for your apps script project.Update the file as shown below:

Save the files.
Go to the terminal and enter the following command to push the files back to Google servers:

Then enter the following command to open the project in the browser

This should open your browser with your new web app.

Creating the Bundle – using WebPack and Babel

Our next step is to use ES6 in Apps Script. We will use babel to cross compile from ES6 and will use webpack to create a bundle from the generated code.

I have a sample Apps Script Project here:

https://github.com/gsmart-in/AppsCurryStep1

Let’s look into the project structure.

The “server” sub-folder contains the code. api.js file contains the functions that we expose to Apps Script.

In the file lib.js we have es6 code. From this lib module, we can import other es6 files and npm packages.

We use webpack to bundle the code and babel to do the cross compilation.

Let us now look at the webpack.gas.js file:

This is the webpack configuration file. In summary, what this configuration file tells webpack is

  • Compile the server/lib.js file in to Javascript compatible with old Javascript using babel. Then place the bundle in a folder “dist”
  • Copy the file api.js without any change to output folder ‘dist’
  • Copy some configuration files (appsscript.js and .clasp.json to the output folder ‘dist’)

One important thing to notice is these lines:

This means that webpack will expose a global variable AppLib through which you can access the classes and functions exported in the bundle.

Now see the file api.js.

See server/lib.js file

We are using Object.assign() which is not supported by Apps Script. When babel cross-compiles lib.js, it will generate compatible code that works on Apps Script.

Let us now see package.json

When you run

Webpack compiles and bundles the lib.js code (and any additional modules you have imported) in to a single javascript file and places it in the “dist” folder.

Then we can just use “clasp” to upload the code .

See the script “deploy” in package.json.

It runs webpack , then does “clasp push” and “clasp open”

Deploying “AppsCurryStep1”

If not done, clone the sample project repository locally

Open terminal and go to the folder AppsCurryStep1

run the following command

This will create a standalone script project in your account

Now run :

This will open your web app in your browser

Integrating npm modules with your Apps Script Project

One of the limiting features of Apps Script is that there is no easy way to integrate npm like packages to your project.

For example, you may want to use momentjs to play with date, or lodash utility functions in your script.

There, indeed, is a Library feature in Apps Script but that option has several limitations. We will not explore the library option of Apps Script in this article; we will install npm modules and bundle those modules using webpack to create an Apps Script compatible package.

Since we already started using webpack to create bundles that we can integrate to apps script, it should be easier now to add some npm packages. Let us start with momentjs

Open terminal, go to the AppsCurryStep1 folder you created in the last step and add momentjs to the mix

Now let us use some momentjs features in our Apps Script project.

Let us add a new function in lib.js

Hint: Don’t forget to export the new function

Now let’s use this new function in api.js

Go to the command line and enter

The updated script should open in the browser and print today’s date

There is not much fun getting todays date. Let us add another function that has a little more to do

Now in api.js update doGet() and call getDaysToAnotherDate()

Next, let us add lodash to the mix

First , run

Let us add a random number generator with the help of lodash

Let us call this new function from api.js

Deploy the project again

You should see the random numbers in your web app page online

The source of this part 2 (integrated with npm module) is available here:
https://github.com/gsmart-in/AppsCurryStep2

Next steps

Now that it is so easy to add npm packages to your Apps Script project, we can start making some npm packages

Packages that wrap the Google APIs, Gmail, Google Sheets, Google Docs, other public APIs Oh we are going to have lots of fun!

Another important half is remaining though. So far we have seen only a simple text interface for our web apps. How about using modern CSS frameworks, bootstrap, bulma, material design, and also VueJS, React and building some single Page Web apps on Apps Script? Yeah, we will do it. We will use bootstrap, Vuejs in client side and Apps Script on server side and will build a single page app.

How exciting is that? Stay tuned for the next part of this series

Related Posts
No related posts for this content

Leave a Reply 0 comments

Leave a Reply: