Create a simple Approval process via Email with Google Form and Google Apps Script (Web Apps Deployment)
TL:DR: At the end of this article, you will know how to create a workflow app, based on a Google Form that send an email to be approved or deny including logging of who is approving or denying with Apps Script. You will have discover how you can:
- Trigger Apps Script onFormSubmit
- Publish your script as a web-app
- Generate emails based on HTML templates
- Generate an UUID to be used for following request sent
Some context
I’m working for Veolia and we use Google Workspace. In my IT departments, we have quite often ask to handle workflow approval as simple as clicking on a button in an email to approve or deny a request.
To achieve those kind of use cases in Google Apps Script, web-apps can comes to your rescue. It is a very simple way to design a solution, a Minimum Viable Product, quickly.
Here is what we will build:
Why a web-app is needed ?
In this scenario, you have a script that contains / does something you want only you to be get access to.
— It could includes : API calls, edit files/folders in your Google Drive for example. —
- You want to allow/share it : 1. Only to specific people (a Google Group?), or, 2. make it available in your organisation / or publicly.
- AND At the same time you don’t want to share this full scope of what your script is actually doing, i.e, it must still be executed by “you”. As an extra you might still want to know who is doing what so you will still keep a track of the email address of the person who reach your web-app.
- Your database will be a Google Sheet.
NB: As I use mostly Apps Script for internal needs in my company I have no experience in public sharing web-apps.
Requirements for web apps in Apps Script
A script can be published as a web app if it meets these requirements:
- It contains a
doGet(e)
ordoPost(e)
function. - The function returns an HTML service
HtmlOutput
object or a Content serviceTextOutput
object.
Learn more here.
Here we go !
Part 1 — On Form Submit
All source are here:
- Make a copy of the sample Google Form.
- Make a copy of the Google Sheet + Apps Script.
- Code repository on GitHub
1 — Create your form, link it to a Google Spreadsheet
2 — First part of our process: onFormSubmit send email to the approver
This script needs to be triggered onFormSubmit so :
1 — Click on “Triggers” on the menu
2 — Click on “Add trigger” button
3 — Add “autoFillGoogleDocForm” as “onFormSubmit”
Now each time someone fill a form the code bellow will be triggered.
It collects information submitted on the Google Sheet linked to the Google Form, generate a UUID, add some information on the line to manually trigger if needed “Approve” or “Deny” function.
To go to this phase, we need to deploy the other part of the script that will be used to allow our end-users to click on the link and “Approve” / “Deny” their request.
Part 2— Deploy your web-app
Now you will have to click on “Deploy”, then “New deployment” to publish your script. It will generates a URL that we will use and add as a Constraint in our script.
1 — Click on Deploy, then New Deployment
2 — Configure your deployment
You can add a name, and define who can access it. For our usecase I need to collect “who” is clicking on the button so I want to know their Gmail address. You can set “Everyone within Corporate organization” or “Anyone with Gmail account”
2 —Now copy the web app URL generated into “Constraints.gs”
3 — Past the URL into “Constraints.gs”
Change “INSERT_HERE_DEPLOYURL” variable.
Change “INSERT_APPROVER_EMAIL” variable. It will be the email that will receive with you the approval request. It can also be a Google Groups for example.
Time to test your code, you can now fill a Form and test that the whole flow is working.
But let’s just look at what the emails sent will look like, you can find them in the .html part of the script. Here is the one sent at the start of the process.
The first email is requesting approval to an approver then it will send the approval or deny status to the requester.
It will also generate a UUID that can be used to track the approval status.
— Say generated a purchase order number for example —
Part 3— Time to test
You can fill a Google Form and you should receive an email like that:
When you click on the link to approve or deny you are receiving a simple message to acknowledge we have collected the information.
All information can be found:
1 — on the Google Sheet to receive the approval :
2 — on the Logs also so we can easily track if people try to click many times for example:
So if you click on “Approve” you will have your email appearing on both page and that should be fine.
Let’s wrap up
Now you have build a simple approval process workflow. Feel free to tune it to your own use case.
You can of course tweak it and improve, let me know what you build with it or if you have questions.
Warm thank you to my team in Stockholm. Shout out to Charlie O., Johan B., Oscar K. during this fun and very quick project we did couple of months ago.
PS: Google Apps Script released on March 15th a new feature.
Extra information — Side notes
It is now possible to release multiple deployment with the same script.
This sounds very interesting for some use cases. Maybe this one ?
It reminded me this tweak I have been doing for specific use cases in my organisation and Martin tweet push me to write this article, which is kinda connected to this new release.