简体   繁体   English

获取两个数组,比较并删除 Google Apps 脚本中的重复项

[英]Get two arrays, compare and remove duplicates in Google Apps Script

I have got two lists on a google spreadsheet: 'Existing Companies' and 'New Companies'.我在谷歌电子表格上有两个列表:“现有公司”和“新公司”。 I would like to compare the two and find out which unique entries in 'New Companies' do not exist in 'Existing Companies', get those entries and eliminate from 'New Companies' all other entries.我想比较两者并找出“现有公司”中不存在“新公司”中的哪些独特条目,获取这些条目并从“新公司”中删除所有其他条目。

I have made the following script to do it:我已经制作了以下脚本来做到这一点:

function grabNewCompanies() {

  // grab existing companies list from sheet into an array
  var sh = SpreadsheetApp.openById("sheetID").getSheetByName("sheetName")
  var row = sh.getDataRange().getLastRow()
  var existingCompanies = sh.getRange(2,1,row - 1,1).getValues()
  Logger.log(existingCompanies)

//grab new companies added
  var sh = SpreadsheetApp.openById("sheetID").getSheetByName("sheetName")
  var row = sh.getDataRange().getLastRow()
  var newCompanies = sh.getRange(2,4,row - 1, 1).getValues()
  Logger.log(newCompanies)

  var array = [];  
  for(i=0; i<newCompanies.length; i++) {
    for(j=0; j<existingCompanies.length; j++) {
      if(newCompanies[i][0] !== existingCompanies[j][0]) {
      array.push([newCompanies[i][0]]);
}

Logger.log(array)

    }

I have ran this script but it has failed.我已经运行了这个脚本,但它失败了。 The two arrays ( existingCompanies and newCompanies ) are returned correctly.正确返回了两个数组( existingCompaniesnewCompanies )。

However, the comparison between the two does not seem to be working: it always returns the first element of the newCompanies array, regardless of whether it exists in existingCompanies .但是,两者之间的比较似乎不起作用:它始终返回newCompanies数组的第一个元素,无论它是否存在于existingCompanies

Also, I am unsure about how to ensure that the values pushed into array are not duplicated if newCompanies contains more than one entry which does not exist in existingCompanies .此外,如果newCompanies包含多个在existingCompanies中不存在的条目,我不确定如何确保推入array的值不会重复。

Thank you.谢谢你。

You want to retrieve the difference elements between existingCompanies and newCompanies .您想要检索existingCompaniesnewCompanies之间的差异元素。 If my understand for your question is correct, how about this modification?如果我对你的问题的理解是正确的,那么这个修改怎么样? I think that there are several solutions for your situation.我认为针对您的情况有几种解决方案。 So please think of this as one of them.因此,请将此视为其中之一。

Modification points:改装要点:

  • In the case that your script is modified, it picks up an element from newCompanies and it checks whether that is included in existingCompanies .如果您的脚本被修改,它会从newCompanies选取一个元素,并检查该元素是否包含在existingCompanies
  • If that picked element is not included in existingCompanies , the element is pushed to array .如果该选取的元素未包含在existingCompanies ,则该元素将被推送到array In this modification, I used true and false for checking this.在这次修改中,我使用了truefalse来检查这一点。

This flow is repeated until all elements in newCompanies are checked.重复此流程,直到检查newCompanies中的所有元素。

Modified script 1:修改后的脚本 1:

When your script is modified, how about this?当你的脚本被修改时,这个怎么样?

From: 从:
 var array = []; for(i=0; i<newCompanies.length; i++) { for(j=0; j<existingCompanies.length; j++) { if(newCompanies[i][0] !== existingCompanies[j][0]) { array.push([newCompanies[i][0]]); } } }
To: 到:
 var array = []; for(i=0; i<newCompanies.length; i++) { var temp = false; // Added for(j=0; j<existingCompanies.length; j++) { if(newCompanies[i][0] === existingCompanies[j][0]) { // Modified temp = true; // Added break; // Added } } if (!temp) array.push([newCompanies[i][0]]); // Modified } Logger.log(array)

Modified script 2:修改后的脚本 2:

As other patterns, how about the following 2 samples?作为其他模式,以下 2 个样本如何? The process cost of these scripts are lower than that of the script using for loop.这些脚本的处理成本低于使用 for 循环的脚本。

 var array = newCompanies.filter(function(e) {return existingCompanies.filter(function(f) {return f[0] == e[0]}).length == 0}); Logger.log(array)

or或者

var array = newCompanies.filter(function(e) {return !existingCompanies.some(function(f) {return f[0] == e[0]})}); Logger.log(array)

If I misunderstand your question, please tell me.如果我误解了你的问题,请告诉我。 I would like to modify it.我想修改它。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM