管理日历活动

时间:2018-01-18 07:06:08

标签: google-apps-script

我正在尝试使用脚本自动化我的一个电子邮件地址日历应用程序当预订的事件应该自动接受该事件,如果事件已经预订,则拒绝该事件并发送电子邮件通知。

以下脚本无效,请帮我解决问题。

function processInvites() {
  var calendarId =  'myemail@domain.com'; // this needs to be the email address of the calendar you're monitoring
  var invited = "INVITED";
  var accepted = "YES";
  var accept = CalendarApp.GuestStatus.YES;
  var reject = CalendarApp.GuestStatus.NO;
  var rejection = "Eep - someone else has a booking then..."; //subject line for our email to reject a booking
  
  var calendar = CalendarApp.getCalendarById(calendarId);
  
  var start = new Date();
 // var end = new Date("January 1, 2100 00:00:00 IST");
  var end = new Date ("January 1, 2100 00:00:00 GMT+0530 (IST)")
  var invites = calendar.getEvents(start, end, invited); //find all future invites (up to 2099)
  
  for(var i = 0; i < invites.length; i++){
    Logger.log("Processing: "+invites[i].getTitle());
    //Search for accepted events that overlap this one:
    var conflicts = calendar.getEvents(invites[i].getStartTime(), invites[i].getEndTime(),accepted);
    if(conflicts.length>0){
      Logger.log("Found a potential conflict to: " + invites[i].getTitle());
      Logger.log("Creator is: " + invites[i].getCreators());
      var body = 
            "Hi "+invites[i].getCreators()+",<br><br>"+
            "You tried to make a booking, between:<br><br>"+
            "<b>"+invites[i].getStartTime()+"</b>"+
            "<br>and<br><b>"+
            invites[i].getEndTime()+"</b><br><br>"+
            "Unfortunately it's already booked then by <b>"+conflicts[0].getCreators()+"</b> for <b>'"+conflicts[0].getTitle()+"'</b><br><br>"+
            "Sorry!";
      MailApp.sendEmail(invites[i].getCreators(), rejection, "", {htmlBody: body})       
      invites[i].setMyStatus(reject);
    }
    else{
      Logger.log("No conflict, accepting: " + invites[i].getTitle());
      invites[i].setMyStatus(accept);
    }
  }
};

1 个答案:

答案 0 :(得分:0)

以下getEvents代码不返回任何事件(最后更多详细信息)

var invites = calendar.getEvents(start, end, invited);

所以我的解决方法是确定您被邀请/接受的事件

var invites = calendar.getEvents(start, end)//,{ search : "bday"});
for (var i = 0; i<invites.length; i++){
 if(invites[i].getMyStatus() == CalendarApp.GuestStatus.INVITED)  // for accepted use CalendarApp.GuestStatus.YES
  Logger.log(invites[i].getTitle()) 
}

注意:如果您的活动所有者虽然不起作用

您的最终代码将如下所示(未经测试的代码):

function processInvites() {
  var calendarId =  'myemail@domain.com'; // this needs to be the email address of the calendar you're monitoring
  var invited = CalendarApp.GuestStatus.INVITED;
  var accepted = CalendarApp.GuestStatus.YES;
  var accept = CalendarApp.GuestStatus.YES;
  var reject = CalendarApp.GuestStatus.NO;
  var rejection = "Eep - someone else has a booking then..."; //subject line for our email to reject a booking

  var calendar = CalendarApp.getCalendarById(calendarId);

  var start = new Date();
 // var end = new Date("January 1, 2100 00:00:00 IST");
  var end = new Date ("January 1, 2100 00:00:00 GMT+0530 (IST)")
  var invites = calendar.getEvents(start, end); //find all future invites (up to 2099)

  for(var i = 0; i < invites.length; i++){
    if ( invites[i].getMyStatus() != CalendarApp.GuestStatus.INVITED){
       continue;
    }
    Logger.log("Processing: "+invites[i].getTitle());
    //Search for accepted events that overlap this one:
    var conflicts = calendar.getEvents(invites[i].getStartTime(), invites[i].getEndTime());
    for (var j = 0; j < conflicts.length ; j++){
       if(conflicts[j].getMyStatus() == CalendarApp.GuestStatus.YES)
             break;
    }
    if(j < conflicts.length){
      Logger.log("Found a potential conflict to: " + invites[i].getTitle());
      Logger.log("Creator is: " + invites[i].getCreators());
      var body = 
            "Hi "+invites[i].getCreators()+",<br><br>"+
            "You tried to make a booking, between:<br><br>"+
            "<b>"+invites[i].getStartTime()+"</b>"+
            "<br>and<br><b>"+
            invites[i].getEndTime()+"</b><br><br>"+
            "Unfortunately it's already booked then by <b>"+conflicts[j].getCreators()+"</b> for <b>'"+conflicts[j].getTitle()+"'</b><br><br>"+
            "Sorry!";
      MailApp.sendEmail(invites[i].getCreators(), rejection, "", {htmlBody: body})       
      invites[i].setMyStatus(reject);
    }
    else{
      Logger.log("No conflict, accepting: " + invites[i].getTitle());
      invites[i].setMyStatus(accept);
    }
  }
};

其他细节: 根据{{​​3}},带过滤器的getEvents的语法应该是这样的:

var invites = calendar.getEvents(start, end, {statusFilters : [CalendarApp.GuestStatus.INVITED]})

然而,这也给出了一个空的邀请列表,在我的情况下(我的语法也可能是错误的,我的搜索结果是空的)。