Skip to content

Instantly share code, notes, and snippets.

@ShaneDunn
Created February 4, 2017 07:49
Show Gist options
  • Select an option

  • Save ShaneDunn/4e09bfd114adc11c47cf6ace4576e6c8 to your computer and use it in GitHub Desktop.

Select an option

Save ShaneDunn/4e09bfd114adc11c47cf6ace4576e6c8 to your computer and use it in GitHub Desktop.
A google script to take spreadsheet events (a Football Draw) and load them into the google Calendar
//add a menu when the spreadsheet is opened
function onOpen() {
var ui = SpreadsheetApp.getUi();
// Or DocumentApp or FormApp.
ui.createMenu('Calendar Menu')
.addItem('Update Calendar', 'pushToCalendar')
.addToUi();
}
//push new events to calendar
function pushToCalendar() {
//below are the column ids of that represents the values used in the spreadsheet (these are zero indexed)
//Column containg the round to be played
var vRound = 0;
//Column containg the date of the game
var vDate = 1;
//Column containg the opposition
var vVs = 2;
//Column containg the location of the game
var vlocation = 3;
//spreadsheet variables
var sheet = SpreadsheetApp.getActive().getSheetByName("Draw");
var lastRow = sheet.getLastRow();
var range = sheet.getRange(2,1,lastRow,5);
var values = range.getValues();
//calendar variables
var calendar = CalendarApp.getCalendarById('Calendar_id')
var numValues = 0;
for (var i = 0; i < values.length; i++) {
//check to see if round and vs are filled out - date is left off because length is "undefined"
if ((values[i][vRound].toString().length > 0) && (values[i][vVs].length > 0)) {
//check if it's been entered before
if (values[i][4] != 'y') {
//create event https://developers.google.com/apps-script/class_calendarapp#createEvent
var newEventTitle = 'RFNL Rnd: ' + values[i][vRound] + ' - ' + values[i][vVs];
var newEvent = calendar.createAllDayEvent(newEventTitle, values[i][vDate], {location: values[i][vlocation]});
//get ID
var newEventId = newEvent.getId();
//mark as entered, enter ID
sheet.getRange(i+2,5).setValue('y');
sheet.getRange(i+2,6).setValue(newEventId);
} //could edit here with an else statement
}
numValues++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment