根据数组中的值创建“if”语句

时间:2015-06-12 00:17:28

标签: javascript google-apps-script

我试图找出一个用户在其日历中有一个事件,他们没有被邀请作为嘉宾。我认为我错了的部分是:

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

代码:

function myFunction() {      
    var calendarId = 'user.name@altostrat.com';
    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);
    }
}

1 个答案:

答案 0 :(得分:2)

如果找不到值,则

.indexOf()返回-1,如果找到则返回索引。它不会返回true / false

所以,改变一下:

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

到此:

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