I'm currently trying to automate an auto-reply with Google Apps Script for whenever I am out of office based on my Google calendar events named 'OOF'. Even if everytime I'm getting closer, there's always something that doesn't work. Now when I execute it, there's a new error message. See below:
ReferenceError: myEmail is not defined autoReply @ Code.gs:44
Please know that I'm no coder and I have a really basic knowledge of coding haha
Also, another thing to keep in mind is I prefer sending the auto-reply reply in a new thread of email instead of replying directly to an email. This is one of the reasons why I'm using Google Calendar to automate an auto-reply.
And I don't want to push my luck too much but is there any way to make the code below work if the Google Calendar is not an all day event (i.e.: 12pm to 6pm instead of all day)
See below the code I've been using:
function autoReply() {
Logger.log('Script start');
var strCalendarOOFKey = 'OOF';
var strUserEmailToSetVacationOn = '[email protected]';
// find calendar event
var today = new Date();
var msTodayStart = today.getTime();
var msTodayEnd = today.getTime()+60000;
var unavailableToday = false;
Logger.log('Looking for Calendar trigger "'+strCalendarOOFKey+'" for today '+today.toDateString());
var events = CalendarApp.getDefaultCalendar().getEventsForDay(today, { search: 'OOF' });
for (var i = 0; i < events.length; i++) {
if(events[i].isAllDayEvent() && events[i].isOwnedByMe()) {
Logger.log("Setting vacation response to true due to event titled '"+events[i].getTitle()+"'")
unavailableToday = true;
break;
}
}
Logger.log("Setting Vacation response to: "+unavailableToday.toString());
var jsonVacationSettingsOn = {
"enableAutoReply": true,
"responseSubject": "I'm currently out of the office",
"responseBodyPlainText": "If you need any help while I'm gone contact [email protected]",
"responseBodyHtml": "If you need any help while I'm gone contact [email protected]",
"restrictToContacts": true,
"restrictToDomain": false,
"startTime": msTodayStart,
"endTime": msTodayEnd
}
var interval = 2;
var date = new Date();
var timeFrom = Math.floor(date.valueOf()/1000) - 60 * interval;
var threads = GmailApp.search('is:inbox !label:autoresponded after:' + timeFrom);
var label = GmailApp.getUserLabelByName("autoresponded");
for (var i = 0; i < threads.length; i++) {
var message = threads[i].getMessages()[0];
if (message.getFrom().indexOf(myEmail) < 0 && message.getFrom().indexOf("no-repl") < 0 && message.getFrom().indexOf("bounce") < 0 && message.getFrom().indexOf("spam") < 0) {
Logger.log("Replied now");
// reply
threads[i].reply("", {
htmlBody: htmlbody
});
// label
label.addToThread(threads[i]);
}
}
}