如何使用Google脚本在两个单独的行中检测重复值?

时间:2019-04-13 14:13:45

标签: google-apps-script google-sheets

我正在尝试使用Google Scripts和Google Spreadsheet来检测A列中是否已提及B列中的值。这些列分别位于2个单独的工作表中。由于某种原因,我没有足够的经验来使它正常工作:)最好的谢谢 getNordic

function insert(e,sheet) {
var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/12NpQZBCaw2jmK5oR-zWmvfNxGeMjiAQzB4-RFtX30W4/edit#gid=0");
var sheet = ss.getSheetByName("Sheet1")"
var check2 =sheet.getRange(seclastrow, 1).getValue();
var st = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1gYEedTcsbjNKLl5Fmd1kxROIah5uIq3VHG_O7JKvEs0/edit#gid=0");
var sheett = st.getSheetByName("sheet1");
var data = sheett.getRange("P8:P50").getValues();
var newData = new Array();
for (i in data) {
    var row = data[i];
  /*  TODO feature enhancement in de-duplication
    var date_modified =row[row.length-2];

    var order_key = row[row.length];

    var existingDataSearchParam = order_key + "/" + date_modified; 
   */

    var duplicate = false;

    for (j in newData) {

      var rowNewData = newData[j];

      var new_order_key = rowNewData[rowNewData.length];

      //var newDataSearchParam = new_order_key + "/" + new_date_modified; // TODO feature enhancement in de-duplication

      if(check2 == newData) {
            duplicate = true;

        }

      // TODO feature enhancement in de-duplication
      /*if (existingDataSearchParam == newDataSearchParam){
        duplicate = true;
      }*/

    }
    if (duplicate == true) {
        sheett.getRange(8, 26).setValue("Success");;
    }else {
      sheett.getRange(8, 26).setValue("Not Found");
}
}
var truefalse = sheett.getRange(8, 26).getValue();

 return ContentService
 .createTextOutput(newData)
 .setMimeType(ContentService.MimeType.JAVASCRIPT);

}

1 个答案:

答案 0 :(得分:1)

在两个不同电子表格的不同列之间查找重复项

  

您需要提供ID0,ID1,SheetName0和SheetName1

function findDuplicatesInDifferentSpreadsheets() {
  var ss0=SpreadsheetApp.OpenById("ID0");
  var sh0=ss0.getSheetByName("SheetName0");
  var rg0=sh0.getRange(1,1,sh0.getLastRow(),1);
  var vA0=rg0.getValues();
  var ss1=SpreadsheetApp.openById('ID1');
  var sh1=ss1.getSheetByName("SheetName1");
  var rg1=sh1.getRange(1,2,sh1.getLastRow(),1);
  var vA1=rg1.getValues();
  var dA=[];//duplicate array
  var v0A=vA0.map(function(r){return r[0];});//flatten
  var v1A=vA1.map(function(r){return r[0];});//flatten
  for(var i=0;i<v0A.length;i++) {
    for(var j=0;j<v1A.length;j++) {
      if(v0A[i]==v1A[j]) {
        dA.push(Utilities.formatString('Duplicate Found:<br />SpreadSheet: %s SheetName: %s Row: %s Value: %s<br />SpreadSheet: %s SheetName: %s Row: %s Value: %s',ss0.getName(),sh0.getName(),i+1,v0A[i],ss1.getName(),sh1.getName(),j+1,v1A[j]));
      }
    }
  }
  //Logger.log(dA);                            
  var html=dA.join('<br />');
  var userInterface=HtmlService.createHtmlOutput(html);
  SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Duplicates');//Results Dialog
}

我的数据表

enter image description here