简体   繁体   English

Javascript / Google Apps Script比较2个数组,返回没有匹配项

[英]Javascript / Google Apps Script compare 2 arrays, return items with NO match

Target: Compare list of attendees with list of invitees and send email reminder to those who did not attend. 目标:将参加者列表与被邀请者列表进行比较,并向未参加者发送电子邮件提醒。

Method: I am attempting to compare 2 arrays [invited & attended] and return items that are NOT found in each ie who did not attend training so I can trigger an email reminder. 方法:我正在尝试比较2个数组(邀请和参加),并返回每个数组中未找到的项目,即没有参加培训的人,因此我可以触发电子邮件提醒。

I've got myself stuck and am mentally blank to the solution. 我陷入了困境,对解决方案一无所知。 My current code compares the 2 arrays and returns matches. 我当前的代码比较2个数组并返回匹配项。 If I set the if statement criteria to if (value[x] !== value[y]) {Logger.log[x]} I am shown rather undesirable output. 如果将if语句条件设置为if(value [x]!== value [y]){Logger.log [x]},则会显示出我不希望看到的输出。

Current function as below: 当前功能如下:

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. 会为修复它的人购买数字咖啡...如果我知道了,我会喝咖啡。

Short answer 简短答案

Google Apps Script compatible version of the script by Redu 谷歌的Google Apps脚本兼容版本的剧本热度

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

Adaptation to the OP case 适应OP案

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, 简短答案中的脚本适用于“简单”数组,我们需要使用以下模式来展平要作为参数传递的对象,在这种情况下,getInvite和getAttend

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

Applying the above to the OP code, 将以上内容应用于OP代码,

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. 如果您复制上述代码,也不要忘记在简短答案部分中复制代码。

References 参考文献

Flatten an array of arrays 展平数组

You may filter then out as follows; 您可以按照以下步骤进行过滤:

 var invited = [1,2,3,4,5,6,7,8,9], attended = [1,5,7,8], missed = invited.filter(e => attended.indexOf(e) === -1); console.log(missed) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Apps 脚本:arrays 中的模糊匹配 - Google Apps Script : Fuzzy match in arrays 如何比较两个 2d Arrays 并按特定顺序返回结果(Google Apps 脚本)? - How to compare two 2d Arrays and return the result in a specific order (Google Apps Script)? 比较两个数组,并仅获取与javascript / google-apps-script中的第1列数据匹配的数组 - Compare two arrays and obtain only one with data matching colum 1 in javascript/ google-apps-script Google Apps 脚本:当 Arrays 之间存在匹配时,合并 Arrays - Google Apps Script: Combine Arrays When there is A Match Between Both Arrays 比较两个数组中的对象并根据javascript中的匹配返回 - Compare objects in two arrays and return based on match in javascript Javascript比较两个不同大小的数组,并返回不在第二个数组中的项目 - Javascript Compare two arrays with different sizes and return the items that are not in second array 如何过滤数组以仅在 Google Apps 脚本/Javascript 中完全匹配 - How to filter array for only exact match in Google Apps Script/Javascript 循环查找匹配的Javascript / Google Apps脚本,但继续 - Javascript / Google Apps Script for loop find match but continue Google 应用程序脚本 - 映射数组 - Google apps script - Mapping arrays 比较Apps脚本中的两个数组不起作用 - Compare two arrays in Apps Script not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM