0

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);
    }
}
2
  • Using the Date constructor to parse strings is not a good idea. Commented Jun 12, 2015 at 1:21
  • You can just do if (!guestArray.indexOf(claendarId)) instead of if (guestArray.indexOf(calendarId) == false)and if (GetValue()) instead of if (GetValue() == true) Commented May 18, 2018 at 0:46

1 Answer 1

2

.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) {
Sign up to request clarification or add additional context in comments.

1 Comment

personally i think its more readable with indexOf(calendarId) < 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.