Skip to content

Instantly share code, notes, and snippets.

@woodwardtw
Last active January 28, 2017 04:00
Show Gist options
  • Save woodwardtw/8f01998291eb737b4204 to your computer and use it in GitHub Desktop.
Save woodwardtw/8f01998291eb737b4204 to your computer and use it in GitHub Desktop.
google script to make g calendar entries from google form submissions
//adapted from https://www.considerednormal.com/2014/04/adding-a-google-calendar-event-using-google-forms/
//this is the ID of the calendar where you want the event to show up, this is found on the calendar settings page of the calendar in question
var calendarId = "YOUR_SECRET_CALENDAR_ID_GOES_HERE";
//below are the column ids that represents the values used in the spreadsheet (these start with 1 rather than 0 like on some things)
//start date - make sure you also check the time box in the Google form (that's new- at least to me)
var startDtId = 5;
//end date - make sure you check the time box in the Google form
var endDtId = 7;
//title of the event
var titleId = 2;
//description
var descId = 8;
//location
var locId = 9;
//timestamp of the form submission
var formTimeStampId = 1;
//autologs the username based on google account- this is a form setting which helps you restrict who can make calendar entries but may be overkill
var userId = 10
function getLatestAndSubmitToCalendar() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var lr = rows.getLastRow();
//date/time is entered here but the pattern is sheet.getRange(lr,VARIABLE,1,1)getValue();
var startDt = sheet.getRange(lr,startDtId,1,1).getValue();
var endDt = sheet.getRange(lr,endDtId,1,1).getValue();
var userName = sheet.getRange(lr,userId,1,1).getValue();
var subOn = "Submitted on: "+sheet.getRange(lr,formTimeStampId,1,1).getValue();
var desc = sheet.getRange(lr,descId,1,1).getValue()+"\n"+subOn+"\n" +"Added by: "+ userName;
var title = sheet.getRange(lr,titleId,1,1).getValue();
var loc = sheet.getRange(lr,locId,1,1).getValue();
createEvent(calendarId,title,startDt,endDt,desc,loc);
}
function createEvent(calendarId,title,startDt,endDt,desc,loc) {
var cal = CalendarApp.getCalendarById(calendarId);
var start = new Date(startDt);
var end = new Date(endDt);
var loc = loc;
var event = cal.createEvent(title, start, end, {
description : desc,
location : loc
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment