Manage event registration with Apps Script, Google Calendar and Google Forms
Using Google Calendar to setup a meeting is really easy and works like a charm for business or personal needs. But sometimes you want to manage event registration for an event, a training and in this case you need to share a form and register the participant after the submission. Hopefully Google Apps Script is at the rescue.
Global idea
The use case is really simple: we want to create an event and allow people to register to the event after filling up a form. The form will allow us to gather some additional information before to add the user to the event. For some events like trainings we also want to stop registration when session is full.
Schema of program
User will connect to the Google Form to register to the event, then after submission Apps Script will process the request to add user to event and if we define a maximum attendees will close the Google Forms.
To process the request after submission we use Apps Script Trigger onForm Submit. Triggers are really great to automate tasks for a workflow with Google Forms, they can also be used as Cron jobs.
Setting up the Google Forms
You can create the form as you want you just have to add an email field.
For the email field you can :
- If the form is public you have to add a field in the form, name of the field will have to be added in the parameters, see code below.
- Alternatively you can also check the option to collect email, the code will check if this option is selected.
- If you publish the form for a G Suite domain you can just check the option to get email of connected user and restrict form to your domain.
Setting up the event
You will have to create the event manually and in the script we will search for the event by ‘event name’. We recommend you to create an event in your calendar with an explicit name, you can also use an event already created.
Using a Google Calendar event will allow you to add all relevant information in the event description. By doing so, when we will add the user to the event he will get notified with the description in the notification.
Interesting options, don’t forget to hide or not the guest list if needed and block (or not) option to add new guests.
Apps Script code to manage event registration
Copy the script from this link : Event Registration Script or copy/paste from below.
appsscript.json :
{
"timeZone": "Europe/Paris",
"dependencies": {
"enabledAdvancedServices": [{
"userSymbol": "Calendar",
"serviceId": "calendar",
"version": "v3"
}]
},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8"
}
code.js :
/*
* Manage event Registration
* Made with Apps Script, v8 runtime compliant
* Run setup() function first
*/const FORM_ID = '';
const CALENDAR_ID = 'primary';
const EVENT_TITLE = 'testing script';
const MAX_ATTENDEES = 2; // false if you don't want to limit attendees or a number
const FORM_EMAIL_LABEL = 'Email' ; //If you use form email collection don't care about this parameter/*
* Setup function to check event is valid and create the trigger
*/
const setup = () => {
// First we test if event is unique in calendar.
// If not it throw an error.
getEvent() ;
let form = FormApp.openById(FORM_ID);
ScriptApp.newTrigger('eventRegistration')
.forForm(form)
.onFormSubmit()
.create();
}/*
* Function that will run at the onFormSubmit trigger
*/const eventRegistration = (e) => {
// Logger.log(JSON.stringify(e))
const form = e.source ;
const formResponse = e.response;
if(form.collectsEmail()){
var new_email = formResponse.getRespondentEmail();
}else{
var new_email = getEmail(formResponse.getItemResponses());
}
var event = getEvent() ;
console.log('Email : ' + new_email)
if(!event.attendees){
event.attendees = []
}
event.attendees.push({email:new_email})
// Logger.log(event)
var newEvent = Calendar.Events.update(event, CALENDAR_ID, event.id,{sendUpdates:"all"}) ;
Logger.log('stop')
if(MAX_ATTENDEES){
if(countAttendees(event.attendees) >= MAX_ATTENDEES){
form.setAcceptingResponses(false);
}
}
}const countAttendees = (attendees) => {
var count = 0;
for(var i in attendees){
var attendee = attendees[i];
if(attendee.responseStatus === 'declined' ){
// We don't count the user that register and decline event after receiving invitation
count++
}
}
// We add 1 to count the new invitation sent
return count+1;
}/*
* Helper function to get event by name.
*/
const getEvent = () => {
const page = Calendar.Events.list(CALENDAR_ID,{q:EVENT_TITLE}) ;
if(page.items && page.items.length > 0){
if(page.items.length > 1){
throw 'We find multiple events with the title '+ EVENT_TITLE;
}
}else{
throw 'We don`t find any event with this event title !!!' ;
}
Logger.log(page)
return page.items[0]
}/*
* Helper function to retrieve email of user
*/
const getEmail = (itemResponses) => {
for (var j = 0; j < itemResponses.length; j++) {
var itemResponse = itemResponses[j];
console.log(itemResponse.getItem().getTitle())
if(itemResponse.getItem().getTitle() == FORM_EMAIL_LABEL){
console.log('Email = '+ itemResponse.getResponse())
return itemResponse.getResponse()
}
}
return false;
}
Setting up the Script
We assume you initially created the event and the form.
- Copy/paste the appsscript.json and code.js code
- Enter the values in the variables at the beginning of the script
- Run the ‘setup()’ function, first time you will have to validate scope
- Now test the form
Source code
Source code is available on GitHub : view on GitHub