Google Forms is a great tool--easy to create, easy to use, etc. I was recently at a meetup talking to users about Google Cloud Platform and someone said they wished they could hook a Google Form up to a Google Cloud Function.
After thinking for a second, I thought "this must be possible". Although there's not usually a lot of direct interoperability between G Suite and Google Cloud, I knew that you could write an Apps Script trigger for most Google Docs, which would let you make HTTP requests, and that Cloud Functions can accept arbitrary HTTP requests as events, so there's no reason this shouldn't work.
And in fact, it totally works!
Writing your Cloud Function
There's really nothing special about the Cloud Function you'll create for this: it's just like any other Cloud Function that accepts a HTTP request. Let's assume we're going to be passing JSON back and forth as the payload:
The function would look something like this:
def form_trigger(request):
payload = request.get_json(silent=True)
print(f"Payload was: {payload}")
return "OK"
This will get the JSON from the POST
request, print it to our logs, and just return an "OK". Obviously you can do then whatever you want with the payload at this point: store it in your database, kick off a job, etc.
Deploy that function with gcloud functions deploy form_trigger --trigger-http --runtime python37
and move on to the next step.
Creating your Google Form
The Google Form you create will just be like any other form: you can have multi-part questions, multiple-choice questions, free-form questions, etc.
Creating a script for your form
Here's where we can start connecting the dots. First, literally select the three dots in the menu when you're editing your form:
From this menu, choose "Script editor" to get taken to the Apps Script editor for this form. This should give you a file named Code.gs
with an empty function like so:
function myFunction() {
}
We're going to update it to be something like the following:
// Replace with the URL to your deployed Cloud Function
var url = "<YOUR CLOUD FUNCTION URL>"
// This function will be called when the form is submitted
function onSubmit(event) {
// The event is a FormResponse object:
// https://developers.google.com/apps-script/reference/forms/form-response
var formResponse = event.response;
// Gets all ItemResponses contained in the form response
// https://developers.google.com/apps-script/reference/forms/form-response#getItemResponses()
var itemResponses = formResponse.getItemResponses();
// Gets the actual response strings from the array of ItemResponses
var responses = itemResponses.map(function getResponse(e) { return e.getResponse(); });
// Post the payload as JSON to our Cloud Function
UrlFetchApp.fetch(
url,
{
"method": "post",
"payload": JSON.stringify({
"responses": responses
})
};
);
}
Make sure to update the url
variable with the full URL to your deployed Cloud Function.
Adding a trigger
Now, from the script editor, click on "Edit" > "Current project's triggers" . This will prompt you to give your project a name, and then take you to the G Suite Developer Hub, and show you all the triggers for your project (there should be none).
In the bottom right corner, select "+ Add Trigger" to add a new trigger:
The dialog should default to the onSubmit
function you declared for your form, otherwise select it.
You should also be sure to change the "Select event type" field from "On open" to "On form submit".
This will create a popup window to allow Apps Script to view and modify your form.
Finally, save your trigger and it should appear in the list of triggers.
Conclusion
At this point, your form is fully connected to Cloud Functions! You can submit some test responses and you should see the responses appearing in the logs for your function.
From here, you can hook the function up to other services, or do things conditionally based on the responses, and you shouldn't need to write additional Apps Script to make it happen.