Javascript/Google Apps脚本比较2个数组,返回没有匹配项 [英] Javascript / Google Apps Script compare 2 arrays, return items with NO match

查看:81
本文介绍了Javascript/Google Apps脚本比较2个数组,返回没有匹配项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:将参加者列表与被邀请者列表进行比较,并向未参加者发送电子邮件提醒.

Target: Compare list of attendees with list of invitees and send email reminder to those who did not attend.

方法:我正在尝试比较2个数组[invited&参加],并退回每个未参加培训的人都找不到的项目,这样我就可以触发电子邮件提醒.

Method: I am attempting to compare 2 arrays [invited & attended] and return items that are NOT found in each i.e who did not attend training so I can trigger an email reminder.

我被困住了,在解决方案方面精神上空白.我当前的代码比较2个数组并返回匹配项.如果将if语句条件设置为if(value [x]!== value [y]){Logger.log [x]},则会显示出我不希望看到的输出.

I've got myself stuck and am mentally blank to the solution. My current code compares the 2 arrays and returns matches. If I set the if statement criteria to if (value[x] !== value[y]) {Logger.log[x]} I am shown rather undesirable output.

当前功能如下:

function getAttendees() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheetByName('Attendees');

    var inviteSheet = ss.getSheetByName('Invitees');
    var inviteLRow = inviteSheet.getLastRow();
    var inviteRange = inviteSheet.getRange("K2:K" + inviteLRow);
    var getInvite = inviteRange.getValues();

    var attendLRow = sheet.getLastRow();
    var getAttendRange = sheet.getRange("A2:A" + attendLRow)
    var getAttend = getAttendRange.getValues();

    for (var v = 0; v < getAttend.length; v++) {
        var vn = v + 2;
        for (var e = 0; e < getInvite.length; e++) {
            var en = e + 1;

            if (getAttend[v].toString() !== getInvite[e].toString()) {
                Logger.log(getAttend[v] + " attended training.");
            }
        }
    }
}

如果我可以退回不在与会者"数组中的项目,则可以触发电子邮件功能(很确定我已经在袋子中了.)

If I can return the items not in the "Attendees" array, I can trigger the email function (pretty sure I have that one in the bag already.)

任何帮助将不胜感激. 会为修复它的人购买数字咖啡...如果我发现,我会得到咖啡.

Any help would be greatly appreciated. Will buy digital coffee for whoever fixes it... If I figured it out, I get coffee.

推荐答案

简短答案

脚本的Google Apps脚本兼容版本. /4543207/redu> Redu

Short answer

Google Apps Script compatible version of the script by Redu

function missed(invited,attended){
  return invited.filter(function(e){return attended.indexOf(e) === -1});
}

适应OP情况

简答中的脚本与简单"脚本一起使用.数组,我们需要使用以下模式来平整对象以作为参数传递:在此问题getInvite和getAttend的情况下,

Adaptation to the OP case

The script in the short answer works with "simple" arrays, we need to flatten objects to be passed as arguments, in the case of this question getInvite and getAttend by using the following pattern,

2DArray
  .reduce(function(a, b) {return a.concat(b);}, [])

将以上内容应用于OP代码,

Applying the above to the OP code,

function getAttendees() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheetByName('Attendees');

    var inviteSheet = ss.getSheetByName('Invitees');
    var inviteLRow = inviteSheet.getLastRow();
    var inviteRange = inviteSheet.getRange("K2:K" + inviteLRow);
    //flattened array of invitees
    var getInvite = inviteRange.getValues()
        .reduce(function(a, b) {return a.concat(b);}, []);

    var attendLRow = sheet.getLastRow();
    var getAttendRange = sheet.getRange("A2:A" + attendLRow)
    
    //flattened array of attendees
    var getAttend = getAttendRange.getValues()
        .reduce(function(a, b) {return a.concat(b);}, []);
   
    //Items with no match
    Logger.log(missed(getInvite,getAttend).join(', ')) 

}

备注

如果您复制上面的代码,也不要忘记在简短答案部分中复制代码.

Remarks

If you copy the above code, don't forget to copy the code in the short answer section too.

展平数组数组

这篇关于Javascript/Google Apps脚本比较2个数组,返回没有匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆