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 – it’s available through my library bmAlasql
I also wanted to make it compatible with my fiddler library, as many of you already use it for fiddling with Sheets data. Here’s how it all works and fits together. There’s still a bit of work to do but it’s good enough to get started with.
Getting started
You’ll need
bmAlasql (1OcbRxA2lKJfR49RtAzl_5XN2_jnT6ByzJRKRihv8VXf1leGEGevlLzYf)
and if you’re planning to use fiddler, you’ll need either the vanilla fiddler library
bmFiddler (13EWG4-lPrEf34itxQhAQ7b9JEbmCBfO8uE4Mhr99CHi3Pw65oxXtq-rU)
or the handy wrapper
bmPreFiddler (13JUFGY18RHfjjuKmIRRfvmGlCYrEkEtN6uUm-iLUcxOUFRJD-WBX-tkR)
Let’s start with using bmAlasql directly – that will allow you to create sql tables in memory and play with them directly from Apps Script
Functions
These functions are currently exported
Vanilla SQL
Creating an empty table
not all SQL data types are yet supported but I’ll be adding them over time.
Inserting data
Once you’ve created a table, you can insert data like this.
Executing
Most SQL statements are available via the exec function – and we’ll look at some examples later. The most basic is just to select what’s in the table
Deducing the table structure
Since in Apps Script we’ll mainly be dealing with either spreadsheet values or object arrays, we need a way to quickly create a table from either of these without fiddling around with SQL syntax.
Creating table from an array of JSON objects
Inserting from an array of JSON objects
We can use the same or different data to insert into the created table
Here’s a query on that data
Inserting and creating
There’s a short cut to do both of that at the same time – create and populate a table
Using fiddler
If you’re dealing with spreadsheet data, it may be more convenient just to use fiddler – especially if you’re reading and writing to sheets too. Lets’s start with arrays of spreadsheet values.
and here’s a query on that
Joins
SQL joins are supported too. Let’s create another table
Now we can join that to another table
Manipulating sheets
Now let’s assume that you want to use sql to do things with sheet data – Again I’ll use fiddler here, pulling in a list of all the airports in the world.
Let’s say we wanted to create a new sheet with just the airports in the world that are in the southern hemisphere and are at an elevation above 6000 ft. With fiddler that’s pretty easy.
And here’s the result
We can now do the same thing with SQL
That may not seem like a massive improvement over vanilla fiddler, but let’s say we wanted to miss out some fields, do some averaging and grouping – the whole world of sql syntax is available.
Joining
SQL makes it easy to join across tables too. Let’s say we have 2 sheets – one the list of airports and another with the average elevation of all airports in each each country. We want to calculate, and create a sheet, showing for each airport, the percentage of the airport’s average elevation for the country it’s in. So for example if the average airport in a country is 1000 ft, an airport at 2000ft in that country would be 200%.
result
Summary
These examples just scratch the surface of what you can do using SQL within Apps Script (with or without Sheets being involved), so no doubt there will be more articles on this subject.
Links
bmAlasql (1OcbRxA2lKJfR49RtAzl_5XN2_jnT6ByzJRKRihv8VXf1leGEGevlLzYf)
and if you’re planning to use fiddler, you’ll need either the vanilla fiddler library
bmFiddler (13EWG4-lPrEf34itxQhAQ7b9JEbmCBfO8uE4Mhr99CHi3Pw65oxXtq-rU)
or the handy wrapper
bmPreFiddler (13JUFGY18RHfjjuKmIRRfvmGlCYrEkEtN6uUm-iLUcxOUFRJD-WBX-tkR)
All are also on github under github.com/brucemcpherson
- Currency exchange rates library for Apps Script – optimized for the parsimonious – swop.cx
- Sheets Workbook functions in Apps Script – Date and Time and Temporal primer
- Sheets Workbook functions in Apps Script – Array functions
- Sheets Workbook functions in Apps Script – Database functions
- Sheets Workbook functions in Apps Script – Filter functions
- SQL for Apps Script – it’s here