仅在工作日从Google表格创建重复发生的Google日历活动

时间:2014-08-28 19:54:25

标签: google-apps-script google-sheets google-calendar-api recurring-events

以下代码完美无缺。但是,我现在需要重复发生的事件才会在工作日(M-F)出现。 例如,如果我在2014年9月2日设立定期会议,未来的会议最终会在周末举行。 这些经常性会议如何才能在工作日出现? 例如,如果会议在星期六举行,请将其移至前一天(星期五)。另一方面,如果会议在星期日举行,则将其移至(星期一)之后的第二天。 这有可能吗?

我已经尝试过使用CalendarApp.Weekday.MONDAY等函数,但它最终会从代​​码中编写.addMonthlyRule()函数...

    //    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 个答案:

答案 0 :(得分:0)

您可以使用以下内容获取星期几:

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

星期日是0,星期一是1等。