[英]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. 会为修复它的人购买数字咖啡...如果我知道了,我会喝咖啡。
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});
}
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(', '))
}
If you copy the above code, don't forget to copy the code in the short answer section too. 如果您复制上述代码,也不要忘记在简短答案部分中复制代码。
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.