使用indexOf <0检查时仍在数组中重复

时间:2019-04-25 21:31:13

标签: google-apps-script google-sheets

我正在设置一个脚本,该脚本从某些工作表的某些列中获取所有UUID,但只需要唯一的UUID。我不想包含重复项,因为所有UUID都会触发每天调用次数有限的API调用。

我用以下代码创建了一个脚本:

  • 从某些列中获取所有UUID
  • 创建一个用于存储唯一UUID的数组
  • 使用indexOf <0检查UUID是否重复
// All sheets
  var overdrachtSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Overdrachten");
  var inschrijvingSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Inschrijvingen");
  var uitschrijvingSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Uitschrijvingen");
  var vergaanvraagSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Vergunningaanvraag");
  var zonetoegangSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Zonetoegang");
  var playerSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Spelers");

  // Get all UUID values
  var overdr_uuids = overdrachtSheet.getRange("G8:G").getValues().filter(function(uuid){ return uuid != "" });
  var inschr_uuids = inschrijvingSheet.getRange("C8:C").getValues().filter(function(uuid){ return uuid != "" });
  var uitschr_uuids = uitschrijvingSheet.getRange("C8:C").getValues().filter(function(uuid){ return uuid != "" });
  var vergaanvr_uuids = vergaanvraagSheet.getRange("C8:C").getValues().filter(function(uuid){ return uuid != "" });
  var zonetoeg_uuids = zonetoegangSheet.getRange("C8:C").getValues().filter(function(uuid){ return uuid != "" });

  // Get an array with unique UUID values only
  var unique_uuids = [];
  overdr_uuids.forEach(function(uuid){
    if(unique_uuids.indexOf(uuid[0]) < 0){
      unique_uuids.push(uuid[0]);
    }
  })

  inschr_uuids.forEach(function(uuid){
    if(unique_uuids.indexOf(uuid[0]) < 0){
      unique_uuids.push(uuid[0]);
    }
  })

  uitschr_uuids.forEach(function(uuid){
    if(unique_uuids.indexOf(uuid[0]) < 0){
      unique_uuids.push(uuid[0]);
    }
  })

  vergaanvr_uuids.forEach(function(uuid){
    if(unique_uuids.indexOf(uuid) < 0){
      unique_uuids.push(uuid[0]);
    }
  })

  zonetoeg_uuids.forEach(function(uuid){
    if(unique_uuids.indexOf(uuid) < 0){
      unique_uuids.push(uuid[0]); 
    }
  })

  var setValuesArray = [];
  unique_uuids.forEach(function(uuid){
      setValuesArray.push([uuid]);  
  })

我仍然在setValuesArray:http://prntscr.com/ngxpi5中得到重复的结果。

当我在if(indexOf <0)检查中的.push之前添加一些日志时,我确实看到indexOf的值为0.0或更高,但是如何...

0 个答案:

没有答案