Quick hack to make a readonly JSON API out of a Google sheet, suitable for use with Formsort form flows.
It's kinda like VLOOKUP but in an API form.
Not recommended for use in production as it's rather slow and easy to break compared to reading data from a proper database.
-
Have a Google sheet with your data
-
In Google Sheets, click Tools > Script editor. This opens Google Apps Script
-
Add the Code.gs from this repository
-
Replace the
SPREADSHEET_URLat the top with the URL of your spreadsheet and save. -
In Google Apps Script, click Publish > Deploy as web app...
-
For your settings, choose Me under Execute the app as (letting the app access your spreadsheet), and Anyone, even anonymous under Who has access to the app, so that you can access the API from any browser.
-
Click Update and take note of the resulting URL, which ends in
/exec- that is what you will query to get data.
https://script.google.com/macros/s/{...a bunch of letters...}/exec
The following examples assume that the following data is in your spreadsheet:
| Make | Model |
|---|---|
| BMW | M3 |
| BMW | M4 |
| BMW | M5 |
| BMW | Z1 |
| BMW | Z3 |
| BMW | Z4 |
| Cadillac | BLS |
| Cadillac | ATS |
| Cadillac | Calais |
| Cadillac | Seville |
Just GET the published URL of the App Script:
https://script.google.com/macros/s/{...}/exec
Result:
[
{
"Make": "BMW",
"Model": "M3",
},
{
"Make": "BMW",
"Model": "M4",
}
...
]Note that if you are doing it in a browser, you will have been redirected, so to reload, you will need to manually paste in the URL again
If you want to filter data by a column, just specify the columns and values to filter on in the URL parameters.
For example, if I wanted to retrieve all Cadillac cars, I would append ?Make=Cadillac to my published URL:
https://script.google.com/macros/s/{...}/exec?Make=Cadillac
This results in only rows with Cadillac in the Make column being returned:
[
{
"Make": "Cadillac",
"Model": "BLS",
},
{
"Make": "Cadillac",
"Model": "ATS",
}
...
]Operators beyond equality are not supported.
If you want to retrieve one record per distinct value in a column (akin to DISTINCT ON in SQL), you can specify the distinct URL parameter.
If I wanted to return one record per make, I would add distinct=Make:
https://script.google.com/macros/s/{...}/exec?distinct=Make
This results in only the first record per Make being returned:
[
{
"Make": "BMW",
"Model": "M3",
},
{
"Make": "Cadillac",
"Model": "ATS",
}
...
]You can specify multiple columns to run a distinct on by separating the column names with a comma, eg: ?distinct=Make,Model if each model also had multiple years, and you only needed one row per make x model.
If you have a single spreadsheet with multiple sheets, you can pass the sheet url parameter with the name of the sheet to specify which sheet's data to read from.