John Smith
John Smith

Reputation: 397

Create an 'if' statement based on value from array

I'm trying to find out how ever a user has an event in it's calendar that they are not invited as guest on. The part I think I'm wrong in is this:

if (guestArray.indexOf(calendarId) == false) {

Code:

function myFunction() {      
    var calendarId = '[email protected]';
    var calendar = CalendarApp.getCalendarById(calendarId); 
    var events = calendar.getEvents(new Date('June 1, 2015 00:00:00 CST'), 
                                    new Date('June 5, 2015 23:59:59 CST'));
    for(var i = 0; i < events.length; i++) {
        var ev = events[i];
        var guestList  = ev.getGuestList();
        var guestArray = [];

        for (var n in guestList) {
            var guestEmail = (guestList[n].getEmail());
            guestArray.push(guestEmail);
        }

    if (guestArray.indexOf(calendarId) == false) {
        Logger.log("User not on guestlist!");
        Logger.log("TITLE: "  + ev.getTitle());
        Logger.log("DATE: "   + ev.getStartTime());  
        Logger.log("GUESTS :" + guestArray);
    }
}

Upvotes: 0

Views: 127

Answers (1)

jfriend00
jfriend00

Reputation: 707218

.indexOf() returns -1 if the value is not found and returns the index if it is found. It doesn't return true/false.

So, change this:

if (guestArray.indexOf(calendarId) == false) {

to this:

if (guestArray.indexOf(calendarId) === -1) {

Upvotes: 2

Related Questions