Steve Lowney
Steve Lowney

Reputation: 1

How to create Google Calendar All Day Events from spreadsheet that is update-able without creating duplicates

I have been using the Google Apps Script with code from this question to create update-able (from spreadsheet) calendar events, without creating duplicates.

/**
 * Adds a custom menu to the active spreadsheet, containing a single menu item
 * for invoking the exportEvents() function.
 * The onOpen() function, when defined, is automatically invoked whenever the
 * spreadsheet is opened.
 * For more information on using the Spreadsheet API, see
 * https://developers.google.com/apps-script/service_spreadsheet
 */
function onOpen() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var entries = [{
    name : "Export Events",
    functionName : "exportEvents"
  }];
  sheet.addMenu("Calendar Actions", entries);
};

/**
 * Export events from spreadsheet to calendar
 */
function exportEvents() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var headerRows = 1;  // Number of rows of header info (to skip)
  var range = sheet.getDataRange();
  var data = range.getValues();
  var calId = "YOUR_CALENDAR_ID";
  var cal = CalendarApp.getCalendarById(calId);
  for (i=0; i<data.length; i++) {
    if (i < headerRows) continue; // Skip header row(s)
    var row = data[i];
    var date = new Date(row[0]);  // First column
    var title = row[1];           // Second column
    var tstart = new Date(row[2]);
    tstart.setDate(date.getDate());
    tstart.setMonth(date.getMonth());
    tstart.setYear(date.getYear());
    var tstop = new Date(row[3]);
    tstop.setDate(date.getDate());
    tstop.setMonth(date.getMonth());
    tstop.setYear(date.getYear());
    var loc = row[4];
    var desc = row[5];
    var id = row[6];              // Sixth column == eventId
    // Check if event already exists, update it if it does
    try {
      var event = cal.getEventSeriesById(id);
    }
    catch (e) {
      // do nothing - we just want to avoid the exception when event doesn't exist
    }
    if (!event) {
      //cal.createEvent(title, new Date("March 3, 2010 08:00:00"), new Date("March 3, 2010 09:00:00"), {description:desc,location:loc});
      var newEvent = cal.createEvent(title, tstart, tstop, {description:desc,location:loc}).getId();
      row[6] = newEvent;  // Update the data array with event ID
    }
    else {
      event.setTitle(title);
      event.setDescription(desc);
      event.setLocation(loc);
      // event.setTime(tstart, tstop); // cannot setTime on eventSeries.
      // ... but we CAN set recurrence!
      var recurrence = CalendarApp.newRecurrence().addDailyRule().times(1);
      event.setRecurrence(recurrence, tstart, tstop);
    }
    debugger;
  }
  // Record all event IDs to spreadsheet
  range.setValues(data);
}

I can get it to work but now I want to create an all day event rather than an event with a start and end time.

I have found the codes for all day events here

    // Creates an all-day event for the moon landing and logs the ID.
var event = CalendarApp.getDefaultCalendar().createAllDayEvent('Apollo 11 Landing',
    new Date('July 20, 1969'),
    {location: 'The Moon'});
Logger.log('Event ID: ' + event.getId());

I dont know how to combine the two. Any Help would be much appreciated, I am a noob when it comes to coding!

Edit: Revised code

**
 * Adds a custom menu to the active spreadsheet, containing a single menu item
 * for invoking the exportEvents() function.
 * The onOpen() function, when defined, is automatically invoked whenever the
 * spreadsheet is opened.
 * For more information on using the Spreadsheet API, see
 * https://developers.google.com/apps-script/service_spreadsheet
 */
function onOpen() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var entries = [{
    name : "Export Events",
    functionName : "exportEvents"
  }];
  sheet.addMenu("Calendar Actions", entries);
};

/**
 * Export events from spreadsheet to calendar
 */
function exportEvents() {
  var space = " - "
  var sheet = SpreadsheetApp.getActiveSheet();
  var headerRows = 1;  // Number of rows of header info (to skip)
  var range = sheet.getDataRange();
  var data = range.getValues();
  var calId = "[email protected]";
  var cal = CalendarApp.getCalendarById(calId);
  for (i=0; i<data.length; i++) {
    if (i < headerRows) continue; // Skip header row(s)
    var row = data[i];
    var date = new Date(row[0]);  // First column
    var numb = row[3];
    var title = numb.substring(11,6) +space+ row[1];
    var loc = row[2];
    var id = row[4];              // Sixth column == eventId
    // Check if event already exists, update it if it does
    try {
      var event = cal.getEventSeriesById(id);
    }
    catch (e) {
      // do nothing - we just want to avoid the exception when event doesn't exist
    }
    if (!event) {
      //cal.createEvent(title, new Date("March 3, 2010 08:00:00"), new Date("March 3, 2010 09:00:00"), {description:desc,location:loc});
      var newEvent = cal.createAllDayEvent(title, date, {location:loc}).getId();
      row[4] = newEvent;  // Update the data array with event ID
    }
    else {
      event.setTitle(title);
      event.setLocation(loc);
      // event.setTime(tstart, tstop); // cannot setTime on eventSeries.
      // ... but we CAN set recurrence!
      var recurrence = CalendarApp.newRecurrence().addDailyRule().times(1);
      event.setRecurrence(recurrence, date);
    }
    debugger;
  }
  // Record all event IDs to spreadsheet
  range.setValues(data);
}

spreadsheet

Upvotes: 0

Views: 1679

Answers (1)

Cooper
Cooper

Reputation: 64140

I think something close to this should do it:

function createAllDayEvents() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  var rg=sh.getRange(2,1,sh.getLastRow()-1,sh.getLastColumn());
  var vA=rg.getValues();
  var cal=CalendarApp.getCalendarById("cal id");
  vA.forEach(function(r,i){
    if(r[0]) {
      var date=new Date(r[0]);
      var title=r[1];
      var loc=r[2];
      var desc=r[3];
      var id=r[4];
      //if there is an id already there that means the event has already been created
      if(!id) {
        var event=cal.createAllDayEvent(title, date, {description:desc,location:loc});
        sh.getRange(i+2,5).setValue(event.getId());
      }
    }
  });  
}

This is what my spreadsheet looks like:

enter image description here

And this will delete the events:

function deleteAllDayEvents() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  var rg=sh.getRange(2,1,sh.getLastRow()-1,sh.getLastColumn());
  var vA=rg.getValues();
  var cal=CalendarApp.getCalendarById("[email protected]");
  vA.forEach(function(r,i){
    var id=r[4];
    if(id && r[5]) {
      cal.getEventById(id).deleteEvent();
      sh.getRange(i+2,5).setValue("");
      sh.getRange(i+2,6).setValue("FALSE");//just to reset the checkbox
    }
  })  
}

With a slight modification to the spreadsheet:

enter image description here

I just added a column of checkboxes to the spreadsheet

That seems to work pretty good for me.

Upvotes: 1

Related Questions