AppsScriptPulse

SuperFetch – a proxy enhancement to Apps Script UrlFetch – Desktop Liberation

I’ve written a few articles about JavaScript proxying on here, and I’m a big fan. I also use a lot of APIs, and it can be time consuming to keep on checking the REST documentation for how to call them and deal with the UrlFetch responses. SuperFetch is a proxy for UrlFetchApp to help.

Bruce Mcpherson has been busy again and this latest post introduces ‘SuperFetch’ the new Google Apps Script library which works as a proxy for UrlFetchApp. SuperFetch has some useful additional functionality including: built in caching and compression, standard response format and error handling, and built-in JSON parsing.

SuperFetch also has some useful features often required when using third party APIs including delaying between requests and rate limiting. Bruce has promised some additional posts detailing more complex API configuration options including authentication.

Source: SuperFetch – a proxy enhancement to Apps Script UrlFetch – Desktop Liberation

Simple but powerful Apps Script Unit Test library – Desktop Liberation

Image credit: the JavaScript Code CC-BY Dmitry Baranovskiy

Unit testing your code with this Apps Script Unit test library as you go along will makes it easy to immediately catch errors, and keeping a running test repertoire ensures that you don’t break anything.

When you start developing more complex Google Workspace solutions like add-ons and Chat apps it’s worth considering how you will test, debug and refactor your code. As noted in this post from Bruce Mcpherson the Apps Script community has published a number of different solutions/approaches to unit tests, a number of these appearing in Pulse.  This latest post from Bruce looks at the bmUnitTest library he has developed highlighting how it can be set up and used.

Source: Simple but powerful Apps Script Unit Test library – Desktop Liberation

JSONata – JSON query and transformation language in Google Apps Script

One of the benefits of Google moving Apps Script to the V8 engine is the possibility to drop in existing JavaScript libraries. Max Makhrov recently highlighted on Twitter how JSONata, which can be used to query and transform JSON data can be used in Google Apps Script:

JSONata is a lightweight query and transformation language for JSON data. Inspired by the ‘location path’ semantics of XPath 3.1, it allows sophisticated queries to be expressed in a compact and intuitive notation. A rich complement of built in operators and functions is provided for manipulating and combining extracted data, and the results of queries can be formatted into any JSON output structure using familiar JSON object and array syntax. Coupled with the facility to create user defined functions, advanced expressions can be built to tackle any JSON query and transformation task. – JSONata

JSONata is a solution better understood by trying it out, which you can do thanks to the script project shared by Max on Twitter or on the JSONata website. If you are interested in data query/manipulation solutions for Google Apps Script you might also want to check out our previous post on using AlaSQL for Apps Script.

Update: Max has published JSONata as an Apps Script library 

SheetQuery: An ORM-Like Query Builder for Google Sheets and Google Apps Script

Image credit: Vance Lucas

A simple and small Google Apps Script library for quickly and easily finding and updating records in Google Sheets with a familiar ORM-like syntax

Following our previous post on Converting Google Sheets cell values to an object array, Vance Lucas (@vlucas) got in touch to highlight the SheetQuery library he has created which as well as being able to get Google Sheet data as an object array has some additional nice features for updating cell values. As Vance highlights:

sometimes working with spreadsheets to find and update specific rows of data can be awkward and tedious. There is no direct built-in way to search for specific values in rows by headings. To do this with the Google-supplied APIs, you have to keep track of row index numbers, column index numbers, and arrays of row data while planning your updates. It’s not fun code to write, and it’s relatively error-prone, especially if you are deleting rows, which causes the row index numbers to shift dynamically.

The library includes a .where method which can be used to apply a filtering function to select the rows of a spreadsheet to be read and/or updated. If you are regularly developing scripts that interact with Google Sheets data this can be a great library to keep in mind.

Source: SheetQuery: An ORM-Like Query Builder for Google Sheets and Google Apps Script

How to Make Your NPM Package Available in Google App Script

Boboss74, CC BY-SA 4.0, via Wikimedia Commons

I recently came across GAS — Google App Script and immediately the first hurdle I had to overcome to was make my NPM packages available on there. I had to hop through a couple loops to make this happen. I wanted to call out the steps in this blog so it’ll hopefully help someone out there as well.

In Pulse we’ve previously highlighted a couple of approaches for using NPM libraries in Google Apps Script. The linked source post come from, recent guest on Totally Unscripted, Nima Poulad, Senior Software Engineer at DocuSign. Nima highlights a quick and dirty approach of using eval() on hosted NPM libraries, but goes on to show how Browseify can be used to convert NPM libraries, highlighting some considerations for this approach.

Source: How to Make Your NPM Package Available in Google App Script

String validation for Google Apps Script projects

String validation for Google Apps Script projects

Sourabh Choraria has packaged some of the validator.js methods into a Google Apps Script library. Ported validators currently include isUrl and isEmail, date validators and more. Sourabh has also posted the backstory behind this library.

Source: GitHub – validatorgs/validator.gs: String validation for Google Apps Script projects.

SQL for Apps Script – it’s here – Desktop Liberation

alaa kaddour, CC BY-SA 4.0, via Wikimedia Commons

While working on Sheets Workbook functions – converted to Apps Script I realized that I’d need to tackle the query language at some point, so I figured I may as well go the whole hog and implement a comprehensive SQL variant for Apps Script. Luckily though, I found alasql so with a few tweaks it was ready to go!

A couple Google Apps Script community contributors have shared solutions for using the AlaSQL.js library. Latest come from Bruce Mcpherson, which is included as the source link. If you use Bruce’s fiddler library his post is worth checking out as he provides examples showing how both libraries can be used together. Another version of AlaSQL.js you should look at is Alex Ivanov’s AlaSQLGS which also includes some data and code samples.

Source: SQL for Apps Script – it’s here – Desktop Liberation

A library SDK for APIs in Google Apps Scripts … think of it as the Advanced Services with batch Superpowers

More technically, this library can be used to send http requests via UrlFetchApp.fetch or UrlFetchApp.fetchAll, interacting with api endpoints in raw form. By bringing it down to a lowest layer on this platform, you get the following benefits:

  • All of the options, features, and abilities that are available. No compromises.
  • Ability to batch the requests in bulk. Performance can be significantly improved and run times lowered.

If you’re looking for a way to duck under the 6 minute limit to your scripts, the last bullet point should be particularly interesting.

This library comes from Adam Morris and is well worth exploring the source link to discover more about this solution. If you are particularly interested in how to use this library with Google Services it’s worth checking out the section on Notes on createGoogEndpoint and if you are interested here is a Google Sheet of current Google Endpoint names and versions (created with Spencer Easton’s Google APIs Library script)

Sheets Workbook functions in Apps Script – Date and Time and Temporal primer – Desktop Liberation

Timezones

If you are handling data from sheets it might be useful to use the same logic as is available in the worksheet for common tasks rather than reinventing them. Dates and Times are especially complicated as JavaScript dates are not good with timezones and date arithmetic, especially since spreadsheets and scripts can exist in different timezones from each other. I’m using Temporal – dates, times and timezones and the proposed new Date system for ECMAScript to emulate what Sheets functions do, and I’ll go through the implementation as a learning aid for Temporal as much as a documentation of the functions.

Source: Sheets Workbook functions in Apps Script – Date and Time and Temporal primer – Desktop Liberation

Random and fake test data in Sheets with Google Apps Script – Desktop Liberation

Image credit: Bruce Mcpherson

Image credit: Bruce Mcpherson

Sometimes you need to generate some fake data for a spreadsheet. In this post I’ll cover a few utilities in 2 separate libraries that can help with this.

Bruce Mcpherson has put together a Google Apps Script library which is a port of Faker.js and elements of d3.js to make it easier to generate random data in your projects. You can see the full list of Faker.js methods which includes a wide range from names/addresses to dates to just lorem text. The library can also generate various fake datasets, strings and selectors.

Source: Random and fake test data in Sheets with Google Apps Script – Desktop Liberation