Having automated event creation using submissions through Google Form, it is helpful to have those submitting requests receive a confirmation email. This is part three of my reservation project, part 1 makes the calendar event and part 2 checks for event conflicts. Now on part 3, this code sends an email using another G Suit Service, Gmail. To make the email more appealing I use a bit of HTML giving it a receipt like appearance.
Below is the code. First is the full program, the second code is a function that contains the HTML for the email formatted as a string. I find it helpful to keep the HTML in a separate file. There is also a link to codepen where you can view and edit the HTML part of the code. Feel free to make copies, change and edit any of this to your liking.
Try editing the HTML yourself on codepen.io.
Plain Code
[code]// Calendar Reservation Project
// Kurt Kaiser, 2018
// All rights reserved</pre>
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();
// Calendar to output requests
var calendar = CalendarApp.getCalendarById(‘e6i9tfp012mt4m5ic81v9lisp0@group.calendar.google.com’);
// Creates an object from the last form submission
function getSubmission(){
this.timestamp = sheet.getRange(lastRow, 1).getValue();
this.name = sheet.getRange(lastRow, 2).getValue();
this.reason = sheet.getRange(lastRow, 3).getValue();
this.date = new Date(sheet.getRange(lastRow, 4).getValue());
this.time = sheet.getRange(lastRow, 5).getValue();
this.email = sheet.getRange(lastRow, 6).getValue();
// Adjust time and make end time
this.date.setHours(this.time.getHours());
this.date.setMinutes(this.time.getMinutes());
this.endTime = new Date(this.date);
this.endTime.setHours(this.time.getHours() + 1);
return this;
}
// Check for date conflicts
function getConflicts(request){
var conflicts = calendar.getEvents(request.date, request.endTime);
if (conflicts.length < 1){
request.header = "Confirmation";
request.message = "Your appointment has been scheduled.";
} else {
request.header = "Conflict";
request.message = "There was a scheduling conflict. Please reschedule.";
}
return conflicts.length;
}
// Creates a calendar event using the submitted data
function updateCalendar(request){
var event = calendar.createEvent(
request.name,
request.date,
request.endTime
)
}
function sendEmail(request){
MailApp.sendEmail({
to: request.email,
subject: request.header,
htmlBody: makeEmail(request)
})
}
// ————–Main————–
function main(){
var request = getSubmission();
if (getConflicts(request) < 1){
updateCalendar(request);
}
sendEmail(request);
}
//[/code]
[code]
// Kurt Kaiser
// Calendar Reservation Email
// All rights reserved, 2018
function htmlEmail(request){
return ‘<!DOCTYPE html><html><head><base target="_top"></head><body><div style="text-align: \
center;font-family: Arial;"><div id="center" style=\
"width:300px;border: 2px dotted grey;background:#ececec;margin:25px;margin-left:auto;\
margin-right:auto;padding:15px;"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/6/69/\
Calendar_font_awesome.svg/512px-Calendar_font_awesome.svg.png"\
width="180" style="margin:10px 0px"><br /><div style=" border: 2px dotted grey;background:white;\
margin-right:auto; margin-left:auto;\
padding:10px;"><h2>’ + request.status + ‘</h2><h3>’ + request.message + ‘<br /><br />’
+ request.name + ‘<br />’ + (request.date.getMonth()+1) + ‘/’ + request.date.getDate() + ‘/’ + request.date.getYear() +
‘<br />’ + request.time.toLocaleTimeString() + ‘<br />’ + request.reason + ‘<br /></h3></div>\
</div><div><p style="font-size:12px"\
>Created\ by<a href="https://www.linkedin.com/in/kurtkaiser/"> Kurt Kaiser</a> </p>\
</div></body></html>’
}
//
[/code]
Great videos! I do have one problem… I’m trying to add the “Reason” from the Google Sheet to the “Description” field of the calendar event. Any advice?
Thanks!
What I would do is add setDescription(description) at the end of the updateCalendar function. Haven’t had the chance to try the code below, let me know if it works for you!
Code:
// Creates a calendar event using the submitted data
function updateCalendar(request){
var event = calendar.createEvent(
request.name,
request.date,
request.endTime
)
event.setDescription(request.reason);
}
Thanks for the quick reply! I’ll try this out and let you know what I find.
Keep on making great content!
Hey Kurt
Your recommendation on the “description” feature worked perfectly. Thanks for that!
Hate to bug you again, but I do have another question… I’m trying to send a notification email to my list of guests. Any insight on this?
Thank you again
Where are you getting the guest list? Are you putting it in manually?