Create recurring Google Calendar events from Google Sheets only on weekdays

僤鯓⒐⒋嵵緔 提交于 2019-12-13 05:18:20

问题


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;
  }

回答1:


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.



来源:https://stackoverflow.com/questions/25556956/create-recurring-google-calendar-events-from-google-sheets-only-on-weekdays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!