使用Google App脚本回复日历活动

时间:2016-09-07 16:46:46

标签: google-apps-script google-calendar-api

首先让我表示编程不是我的专长,所以我会尽力解释我正在做的事情。在我的雇主处,我们在免费的Gmail帐户上有公司活动日历,然后我们为每位员工通过Google Apps(付费)分别设置了帐户。我正在处理一个脚本,该脚本从所述日历中提取事件,将其添加到议程样式的电子邮件中,然后将电子邮件发送到Google Apps帐户。我们的目标是允许员工打开电子邮件,点击他/她感兴趣的活动,然后回复活动。值得注意的是,脚本在免费帐户下保存/运行。

我们有大部分代码正常工作但是在打开事件时没有出现RSVP功能。我们确保在查看议程电子邮件时登录Google App帐户,并且该帐户是该活动的受邀嘉宾。我怀疑这是由于我们生成eventURL的方式存在某种类型的身份验证问题。我也相信这不起作用,因为脚本是作为"用户"谁已经拥有日历。如果这两个怀疑是真的,我不确定如何解决它。

以下是您的评论代码。我已删除了HTML格式和我们的私人日历地址。

function ordinal_suffix_of(i) {
    var j = i % 10,
        k = i % 100;
    if (j == 1 && k != 11) {
        return i + "st";
    }
    if (j == 2 && k != 12) {
        return i + "nd";
    }
    if (j == 3 && k != 13) {
        return i + "rd";
    }
    return i + "th";
}

function myFunction() {
  var calendar = CalendarApp.getCalendarById('HIDDEN@group.calendar.google.com');
  var now = new Date();
  var oneWeekFromNow = new Date(now.getTime() + 604800000);
  var twoWeeksFromNow = new Date(now.getTime() + 1209600000);
  var threeWeeksFromNow = new Date(now.getTime() + 1814400000);
  var eventsOneWeek = calendar.getEvents(now, oneWeekFromNow);
  var eventsTwoWeeks = calendar.getEvents(oneWeekFromNow, twoWeeksFromNow);
  var eventsThreeWeeks = calendar.getEvents(twoWeeksFromNow, threeWeeksFromNow);
  var body = '';

  for (i = 0; i < eventsOneWeek.length; i++) {
    var day = ordinal_suffix_of(Utilities.formatDate(eventsOneWeek[i].getStartTime(), "GMT-0400", "d"));
    var splitEventId = eventsOneWeek[i].getId().split('@');
    var eventURL = "https://www.google.com/calendar/event?eid=" + Utilities.base64Encode(splitEventId[0] + " " + "HIDDEN@group.calendar.google.com");
    var eventTitle = eventsOneWeek[i].getTitle();
    var eventStart = Utilities.formatDate(eventsOneWeek[i].getStartTime(), "GMT-0400", "MMMMM");
    eventStart += ' ' + day;
    eventStart += Utilities.formatDate(eventsOneWeek[i].getStartTime(), "GMT-0400", " 'at' hh:mm a");   
    body += '';
}

  for (i = 0; i < eventsTwoWeeks.length; i++) {
    var day = ordinal_suffix_of(Utilities.formatDate(eventsTwoWeeks[i].getStartTime(), "GMT-0400", "d"));
    var splitEventId = eventsTwoWeeks[i].getId().split('@');
    var eventURL = "https://www.google.com/calendar/event?eid=" + Utilities.base64Encode(splitEventId[0] + " " + "HIDDEN@group.calendar.google.com");
    var eventTitle = eventsTwoWeeks[i].getTitle();
    var eventStart = Utilities.formatDate(eventsTwoWeeks[i].getStartTime(), "GMT-0400", "MMMMM");
    eventStart += ' ' + day;
    eventStart += Utilities.formatDate(eventsTwoWeeks[i].getStartTime(), "GMT-0400", " 'at' hh:mm a");          
    body += '';
}

  for (i = 0; i < eventsThreeWeeks.length; i++) {
    var day = ordinal_suffix_of(Utilities.formatDate(eventsThreeWeeks[i].getStartTime(), "GMT-0400", "d"));
    var splitEventId = eventsThreeWeeks[i].getId().split('@');
    var eventURL = "https://www.google.com/calendar/event?eid=" + Utilities.base64Encode(splitEventId[0] + " " + "HIDDEN@group.calendar.google.com");
    var eventTitle = eventsThreeWeeks[i].getTitle();
    var eventStart = Utilities.formatDate(eventsThreeWeeks[i].getStartTime(), "GMT-0400", "MMMMM");
    eventStart += ' ' + day;
    eventStart += Utilities.formatDate(eventsThreeWeeks[i].getStartTime(), "GMT-0400", " 'at' hh:mm a");           
    body += '';
  }

  GmailApp.sendEmail('HIDDEN@email.com', 'Testing Calendar Agenda', body,{'htmlBody':body});
}

提前感谢您的任何见解/帮助,您可以分享!我们非常感谢!

编辑:

我将脚本复制到我的Google Apps帐户,并将代码调整为从我的默认日历中提取。然后我在我的日历上创建了一个新活动,并邀请我的同事参加。当我运行脚本并打开事件时,它会显示一个更改我的RSVP的选项。当我的同事打开相同的链接时,它会显示完全相同的选项,以更改我的 RSVP状态,而不显示他的 RSVP。这确认它未与日历或帐户隔离,并且特定于链接及其生成方式。

0 个答案:

没有答案