Google Forms Apps Script intro and examples

After my recent foray into Monitoring alerts and customer satisfaction surveys I was fortunate enough to have a couple of conversations with other SREs interested in trying out similar approaches and while discussing the specifics of our individual forms the lack of an easy way to share the details became an annoyance. Storing the wording in a shared doc was OK at first but recreating the forms so we could each learn from and tweak each others wording and design quickly became a distraction with the Google Forms web UI taking up most of the time we had to chat. After we called it quits I decided there had to be another way to manage these kinds of forms and so I did a little searching.

Google has something called Apps Script that lets you customise the behaviour of a number of their products with your own JavaScript code. It’s akin to Visual Basic for Applications (VBA) for Microsoft Office but with less Visual Basic and more cursing at the lack of native JS HEREDOCS. So far I’ve only done small experiments translating some of my existing Google Forms but I think it’s a much preferable experience to write some JavaScript that I can version control and write loops in rather than spend more time than I’m comfortable admitting to fiddling with the form builder UI.

It’s very early days but I’ve created a UnixDaemon Google Forms Apps Script repo that shows some examples of forms I’ve used in the past. Each of them has the code I use to build them and a screenshot showing the form itself. I’ll be using these to test some related tooling such as locally running unit tests and uploading to Google Drive if they pass. My current workflow has been to edit the scripts directly in the Apps Script online editor while testing snippets of language features in JSConsole but I’d much rather find a better way of unit testing and gain access to all the existing JavaScript tools.

The ability to use real loops when building forms has been a huge win already. The core of my quarterly health check, which originally consisted of me adding the same 5 answers to roughly 30 one line questions becomes:

var standard_questions = [ ... ];
var standard_answers = [ ... ];

... snip boilerplate ...

var form = FormApp.create('Health Check ' + formatted_date);

for (let index in standard_questions) {
  form.addMultipleChoiceItem()
    .setTitle(standard_questions[index])
    .setChoiceValues(standard_answers)
    .setRequired(true);
}

Working with Apps Script I can now store the code in GitHub, version it and submit pull requests in the same way as my other projects and it’s easier for other people to make their own changes and deploy the forms. This basic attempt at separating the content from the logic aligns with the way we treat our other systems and rather than reading through the UI and adhoc clicking around you can change the configuration variables, albeit still in the source code, and add your own questions or answer formats.

It’s not a finished journey just yet but by spending a few hours playing with Apps Script I think I’ve found a better way to write my own longer or repetitive forms while also being able to easily share them with others. If you find yourself doing a lot with Google GSuite, and especially Forms, I believe investing a little time into the basics of this extension mechanism will pay dividends. Even if you still end up copying the code from a vim buffer to the web UI.