Skip to content

Instantly share code, notes, and snippets.

@rheajt
Created May 19, 2017 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rheajt/ad68e7c10c90690a02e1ba2b172a9a5f to your computer and use it in GitHub Desktop.
Save rheajt/ad68e7c10c90690a02e1ba2b172a9a5f to your computer and use it in GitHub Desktop.
use google forms to schedule your calendar
function onOpen() {
FormApp.getUi().createMenu('SCHEDULER')
.addItem('Setup Form Scheduler', 'setFormId')
.addToUi();
}
function onInstall() {
onOpen();
}
//opens the modal window to set the calendar id NOT the form id as i have named
function setFormId() {
var html = HtmlService.createTemplateFromFile('setFormId');
var CALENDAR_ID = PropertiesService.getScriptProperties().getProperty('CALENDAR_ID');
var calendar = CalendarApp.getCalendarById(CALENDAR_ID);
if(calendar && CALENDAR_ID) {
html.calendarName = calendar.getName();
html.calendarId = CALENDAR_ID;
} else if(!calendar && CALENDAR_ID) {
html.calendarName = 'You don\'t have permissions to the calendar';
html.calendarId = CALENDAR_ID;
} else {
html.calendarName = 'No calendar set';
html.calendarId = 'GET YOUR CALENDAR ID';
}
FormApp.getUi().showModalDialog(html.evaluate(), 'Set Form Id');
}
//save the form id and trigger id to properties service
function saveFormId(id) {
var form = FormApp.getActiveForm();
var triggerId = PropertiesService.getScriptProperties().getProperty('TRIGGER_ID');
PropertiesService.getScriptProperties().setProperty('CALENDAR_ID', id);
if(triggerId) {
var triggers = ScriptApp.getProjectTriggers();
for(var i = 0; i < triggers.length; i++) {
if(triggers[i].getUniqueId() === triggerId) {
ScriptApp.deleteTrigger(triggers[i]);
}
}
}
var triggerId = ScriptApp.newTrigger('onSubmit')
.forForm(form)
.onFormSubmit()
.create();
PropertiesService.getScriptProperties().setProperty('TRIGGER_ID', triggerId);
}
function onSubmit(e) {
var CALENDAR_ID = PropertiesService.getScriptProperties().getProperty('CALENDAR_ID');
var calendar = CalendarApp.getCalendarById(CALENDAR_ID);
var responses = e.response.getItemResponses();
var period, day, cart, room, note, teacher;
for(var i = 0; i < responses.length; i++) {
var itemResponse = responses[i];
var title = itemResponse.getItem().getTitle();
//TODO: fix the way this is handled
//These should be settings that you can configure
//Needs to take multiple select items into account
if(title === 'Cart') {
cart = itemResponse.getResponse();
}
if(title === 'Teacher') {
teacher = itemResponse.getResponse();
}
if(title === 'Room') {
room = itemResponse.getResponse();
}
if(title === 'Date') {
day = itemResponse.getResponse();
}
if(title === 'Period') {
period = itemResponse.getResponse();
}
if(title === 'Note:') {
note = itemResponse.getResponse();
}
}
for(var j = 0; j < period.length; j++) {
var scheduledDay = getTimes(period[j], day);
var eventTitle = 'Period ' + period[j] + ' - ' + cart + ' - Room ' + room;
calendar.createEvent(eventTitle, scheduledDay.start, scheduledDay.end)
.setDescription(note);
}
}
/**
* takes param date string in format 'YYYY-MM-DD'
* returns javascript date object
*/
function getDateObj(date) {
date = date.split('-');
var year = date[0];
var month = date[1] - 1;
var day = date[2];
date = new Date(year, month, day);
return date;
}
/**
* params period from the multiple select item and day in format 'YYYY-MM-DD'
*/
function getTimes(period, day) {
var times = {};
var start = getDateObj(day);
var end = getDateObj(day);
switch(period) {
case 'A':
start.setHours(8);
end.setHours(9);
break;
case 'B':
start.setHours(9);
end.setHours(10);
break;
case 'C':
start.setHours(11);
end.setHours(12);
break;
case 'D':
start.setHours(12);
end.setHours(13);
break;
default:
break;;
}
return {start: start, end: end};
}
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<style>
label {display: block;}
img {width: 100%;}
</style>
</head>
<body>
<label><b>Calendar Name: </b>
<? if(calendarName) { ?>
<?= calendarName ?>
<? } ?>
</label>
<input type="text" id="form-id" value="<?= calendarId ?>" />
<button type="button" onclick="submitID()">Submit</button>
<script>
function submitID() {
var id = document.getElementById('form-id').value;
google.script.run
.withSuccessHandler(google.script.host.close)
.saveFormId(id);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment