Hello All,
I posted this on Stack Overflow but figured I would also reach out to this community.
Let me first express that programming is not my speciality so I will do my best to explain what I'm working on. At my employer, we have a company activity calendar on a free Gmail account and then we have separate accounts through Google Apps (paid) for each employee. I'm working on a script that pulls the events from said calendar, adds them to an agenda-styled email, and then sends the email to the Google Apps accounts. Our goal is to allow the employee to open the email, click an event he/she is interested in, and then RSVP to the event. It might be worth noting, the script is saved/running under the free account.
We have most of the code working but the RSVP feature is not appearing when the event is opened. We are making sure that the Google App account is logged in when the agenda email is viewed and that the account is an invited guest to the event. I suspect this is due to some type of authentication issue in the way we are generating the eventURL. I also believe this isn't working because the script is running as the "user" who already owns the calendar. If these two suspicions are true, I'm not sure how to fix it.
Here is the code for your review. I've removed the HTML formatting and our private calendar addresses.
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('
[email protected]');
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] + " " + "
[email protected]");
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] + " " + "
[email protected]");
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] + " " + "
[email protected]");
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('
[email protected]', 'Testing Calendar Agenda', body,{'htmlBody':body});
}
Thank you in advance for any insight/help you can share! We greatly appreciate it!