Google Apps脚本:比较数组的唯一值

时间:2018-08-01 02:50:15

标签: javascript google-apps-script

我正在使用HERE提供的解决方案来比较两个数组。提供的示例将在两个数组中找到的值返回到Array1(same),仅将在一个或另外两个Array2中找到的值(diff)返回。

问题::将其应用于自己的脚本时,valuesDATA不返回任何内容,valuesCheckSeeding返回两个数组中的所有值

期望的结果:我有两个数组,我想从中创建第三个数组,或者只从第一个数组valuesDATA中选择不存在于数组中的值。第二,valuesCheckSeeding。使用上述解决方案,我试图将valuesCheckSeedingvaluesDATA中找不到的所有值都推送到valuesDATA中。

valuesDATA的示例:“ U09 F 哈福德FC希尔/希利-A 医学博士 CMSA女孩周六U09 A / B北 总理-顶 TID0118“

我在做什么错?我修改了循环中的matchfound==falsematchfound=true,但仍然不能达到预期的效果。

最相关的片段

var matchfound = false;

for (var i = 0; i < valuesDATA.length; i++) {
  matchfound=false;
    for (var j = 0; j < valuesCheckSeeding.length; j++) {
      if (valuesDATA[i] == valuesCheckSeeding[j]) {
        valuesCheckSeeding.splice(j, 1);
        matchfound=true;
        continue;
        }
        }
          if (matchfound==false) {
            valuesCheckSeeding.push(valuesDATA[i]);
            valuesDATA.splice(i, 1);
            i=i-1;
            }
          }

根据下面的评论/答案编辑的替代文字

//UPDATE SEEDING SHEET
function updateSeedingSheet() {

var today = Utilities.formatDate(new Date(),Session.getScriptTimeZone(), "MM/dd/yyyy hh:mm a");   

//INPUT SHEET INFO
var inputCurrentRow = 4;
var inputCurrentColumn = 20;
var inputNumRows = 1000;
var inputNumColumns =1;

var ssInput = SpreadsheetApp.openById('1Wzg2BklQb6sOZzeC0OEvQ7s7gIQ07sXygEtC0CSGOh4');
var sheetDATA = ssInput.getSheetByName('DATAREF');
var rangeDATA = sheetDATA.getRange(inputCurrentRow, inputCurrentColumn, inputNumRows, inputNumColumns);
var valuesDATA = rangeDATA.getValues();


//SEEDING SHEET INFO
var seedingCurrentRow = 4;
var seedingCurrentColumn = 1;
var seedingNumRows = 1000;
var seedingNumColumns = 1;

var ssSeeding = SpreadsheetApp.openById('1DuCHeZ3zba-nHq-7vYTrylncPGqcA1J9jNyW9DaS3mU');
var sheetSeeding = ssSeeding.getSheetByName('Seeding');
var rangeCheckSeeding = sheetSeeding.getRange(4, 102, 1000, 1);
var columnToClear = sheetSeeding.getRange(seedingCurrentRow, seedingCurrentColumn, seedingNumRows, seedingNumColumns);
var valuesCheckSeeding = rangeCheckSeeding.getValues();


//METHOD TO FILTER
valuesCheckSeeding = valuesCheckSeeding.map(function(e){return e[0];}); //flatten this array
var filteredArr = valuesDATA.filter(function(e){
    return !(this.indexOf(e[0])+1);
},valuesCheckSeeding);
Logger.log(filteredArr);
Logger.log(filteredArr.length);

var rangeSeeding = sheetSeeding.getRange(seedingCurrentRow, seedingCurrentColumn, filteredArr.length, seedingNumColumns);

sheetSeeding.getRange('A1').setValue(today);
columnToClear.clearContent();
rangeSeeding.setValues(filteredArr);



/*
//ALTERNATIVE METHOD USING LOOPS
for (var i = 0; i < valuesDATA.length; i++) {
    for (var j = 0; j < valuesCheckSeeding.length; j++) {
      if (valuesDATA[i][0] == valuesCheckSeeding[j][0]) {
        valuesDATA.splice(i, 1);
        i--; //account for the splice
        break; //go to next i iteration of loop
          }
       }
     }
          Logger.log("VALUES DATA:" + valuesDATA);
          Logger.log("VALUES CHECK SEEDING: " + valuesCheckSeeding);         

//sheetSeeding.getRange('A1').setValue(today);
//rangeSeeding.clearContent();
//rangeSeeding.setValues(valuesDATA); //INCORRECT RANGE HEIGHT, WAS 71 BUT SHOULD BE 1000 - Is splice affecting this? 

*/

}//END FUNCTION

1 个答案:

答案 0 :(得分:1)

  • 您正在检查2D阵列。您需要使用Service= Review of Inpatient Hospital Care Description= When your condition requires you to be an inpatient Description= If payment determination criteria are not met. If we inform you that you do not meet payment determination criteria. Service= Ambulatory Surgical Center (ASC) Description= Covered, including operating rooms, surgical supplies. Service= Hospital Ancillary Services Description= Covered, including surgical supplies, hospital anesthesia services. Service= Hospital Room and Board Description= Covered, including:Semi-Private Rooms. If you are hospitalized at a participating facility. Description= Private Rooms.At Participating Hospitals. Description= At Nonparticipating Hospitals: Description= Newborn nursery care. Covered for the baby's nursery care after birth. Description= Please note: Services at nonparticipating and out-of-state post-acute. Service= Intensive Care Unit/Coronary Care Unit. Description= Covered. Service= Intermediate Care Unit Description= Covered. Service= Isolation Care Unit Description= Covered. [i][0]
  • 您只能尝试拼接[j][0]

尝试

valuesDATA

或者,尝试

for (var i = 0; i < valuesDATA.length; i++) {
    for (var j = 0; j < valuesCheckSeeding.length; j++) {
      if (valuesDATA[i][0] == valuesCheckSeeding[j][0]) {
        valuesDATA.splice(i, 1);
        i--; //account for the splice
        break; //go to next i iteration of loop
        }
     }
}
Logger.log(valuesDATA);