06 February 2020

Now Write your Google Scripts using Modern JavaScript ES6


Google Apps Script has received a significant upgrade ever since it was first released to the public more than a decade ago. Apps Script now uses the V8 JavaScript Engine - it is the same runtime that is used inside the Google Chrome browser and the popular Node.js environment.

This essentially means:

  • Developers can write code using modern JavaScript syntax like Arrow Functions, Classes, Array Destructuring, Template Literals and more.
  • The V8 Engine is fast, powerful and continuously improving, It will likely improve the performance and memory utilization of your Google Scripts.
  • Developers can use new JavaScript ES6 features like Symbols, Iterators, Generators, Promises, Maps, Sets and Proxies that weren’t available in the previous version of Google Apps Script.

Chrome V8 JavaScript Engine

Any new projects that you create inside the Google Apps Script editor automatically use the new V8 runtime.

V8 JavaScript Runtime in Google Apps Script

If you would like to upgrade any old project to use V8, go to the Run menu and choose “Enable new Apps Script runtime powered by V8.” If you do not see this option yet, add a new runtimeVersion field in your project’s manifest file with the value of V8. You can set the value to DEPRECATED_ES5 to switch to the old version that uses the Mozilla’s Rhino JavaScript engine.

Tip: Type script.new in your browser to quickly create a new Google Apps Script project in your browser. (Source)

ES6 Modules - The Missing Part

ES6 introduced the concept of modules in JavaScript which allows developers to write reusable code that is also easier to refactor and maintain. You can break your program into separate files (modules) and then import them into other modules using import-export statements.

The new Google Apps Script environment doesn’t support ES6 modules.

The other big change is that functions become available based on the sequence of files in the script editor. Let me explain.

Say your Apps Script project has a lot of files and you have created two functions of the same name but they are located in different files. Apps Script won’t complain but when you can call this function, the one that is defined in the bottom-most file of the project will be invoked.

Google Apps Script Modules

If you prefer the ease of working with ES6 Modules, the Apps Script Starter kit can help. You can write code locally inside Visual Studio Code, bundle the modules into a single file with Webpack and then push the bundle to the cloud automatically with Clasp.

The starter kit has also been updated to use the new V8 Runtime. Watch this YouTube video to learn how to develop with Google Scripts using the Starter Kit.

Learn Modern JavaScript ES6

Coming back to Javascript, I do have a few recommendations that will help improve your understanding of ECMAScript 6.

  • Understanding ES6 - This online book covers all the new features that have been added to the JavaScript language since ES6.
  • ES6 Udacity - A detailed video course that covers all aspects of ES6, complete with quizzes and doesn’t cost a penny.
  • Exploring ES6 - Deep dive into the core features of ES6.
  • Mozilla Docs - Once you are comfortable with the core concepts, this is the best reference site for ES6 with examples.
  • If you prefer premium courses, check out the ones by Maximilian Schwarzmüller and Wes Bos.

Also see: The Best Online Teachers for Web Development


No comments:

Post a Comment