0

The following code below works perfectly. However, I now need to have recurring events to appear only during the weekdays (M-F). If for example I set up a recurring meeting on 9/2/2014, future meetings will eventually fall on a weekend. How can these recurring meetings only show up on weekdays? For example, if meeting falls on a Saturday, move it to the day before (Friday). On the other hand, if meeting falls on a Sunday, move it to the day after (Monday). is this possible?

I have tried using the CalendarApp.Weekday.MONDAY, etc...function but it ends up writing over the .addMonthlyRule() function from the code...

    //    Date | Title | Start Time | End Time | Location | Description | Recurring (months) | EventID
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 = "airliquide.com_ro3r20vk2rhm506fr2toq4vh5c@group.calendar.google.com";
  var cal = CalendarApp.getCalendarById(calId);
  for (i in data) {
    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 = setTimeToDate(date,row[2]);
    var tstop = setTimeToDate(date,row[3]);
    Logger.log('date = '+date+'tstart = '+tstart+'  tstop = '+tstop);
    var loc = row[4];
    var desc = row[5];
    var times = row[6]
    var id = row[7]; 
    // Check if event already exists, update it if it does
    try {
      var event = cal.getEventSeriesById(id);
      event.setTitle('got you');// this is to "force error" if the event does not exist, il will never show for real ;-)
    }catch(e){
      var newEvent = cal.createEvent(title, tstart, tstop, {description:desc,location:loc}); // create a "normal" event
      row[7] = newEvent.getId();  // Update the data array with event ID
      Logger.log('event created');// while debugging
      var event = cal.getEventSeriesById(row[7]);// make it an event Serie
    }
    event.setTitle(title);
    event.setDescription(desc);
    event.setLocation(loc);
      var recurrence = CalendarApp.newRecurrence().addMonthlyRule().interval(times)
      //.onlyOnWeekdays(
      //[CalendarApp.Weekday.MONDAY, CalendarApp.Weekday.TUESDAY, CalendarApp.Weekday.WEDNESDAY, CalendarApp.Weekday.THURSDAY, CalendarApp.Weekday.FRIDAY]);
      event.setRecurrence(recurrence, tstart, tstop);// we need to keep start and stop otherwise it becomes an AllDayEvent if only start is used
      }
  range.setValues(data);
}

function setTimeToDate(date,time){
  var t = new Date(time);
  var hour = t.getHours();
  var min = t.getMinutes();
  var sec = t.getSeconds();
  var dateMod = new Date(date.setHours(hour,min,sec,0))
  return dateMod;
  }
Arsene de Conde
  • 41
  • 3
  • 10

1 Answers1

0

You can get the day of the week by using the following:

var d = new Date();
var n = d.getDay();

Sunday is 0, Monday is 1 etc.

AshClarke
  • 2,990
  • 5
  • 21
  • 27
  • I see but how can this be utilized to have the recurring events only show up on weekdays? – Arsene de Conde Aug 29 '14 at 12:47
  • @Arsene de Conde - this won't be possible using your logic or at least very complex since each occurrence should be checked against day of week and modified for this specific event (which is not possible with script I think)... I'd suggest you create individual events from a spreadsheet in which dates could be controlled accurately regarding the day and the date (because there are also holidays to check for !). Seeing your other recent posts I guess that's what you are doing right ? – Serge insas Sep 11 '14 at 11:34
  • Yes it is. Thanks Serge! That makes sense. – Arsene de Conde Sep 11 '14 at 13:55