根据主文件中的2个值删除其他工作表中的行

时间:2014-11-10 10:33:05

标签: google-apps-script google-sheets

是否可以根据2个值删除其他工作表上的行?说我有3张。在主工作表(工作表1)中,将有2列:分支和经理与其余工作表相同。

SAMPLE SPREADSHEET HERE

示例数据:

第1页:(主页)

  ---   BRANCH    ---   MANAGER  ---
      California       Tom Chang
      Brooklyn         Jon Sieg
      New York         Raq Craig

第2页

  ---   BRANCH    ---   MANAGER  ---
      California       Jane Cali
      California       Tom Chang
      San Francisco    James Chao

第2页

  ---   BRANCH    ---   MANAGER  ---
      California       Jane Cali
      California       Tom Chang
      New York         Daniel Trevor

应该发生的是:

分支列值不应在所有工作表中重复。所以我们需要做的是删除工作表2和3上的行,如果分支列与主工作表相同(工作表1)如果管理器不相同/相等则。因此,在上面给出的数据中,Branch California和经理Tom Chang存在于所有工作表中,因此不应该触及。但加利福尼亚分公司在剩余的2张纸上重复了另一位经理。因此,应在表2和表3中删除加州---- Jane Cali行。

来自这个post借来的剧本,但似乎无法奏效。这里:

function removeDupsInOtherSheets() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var mainsheet = ss.getSheetByName("Sheet3").getDataRange().getValues();   
  var sheet2 = ss.getSheetByName("Sheet2").getDataRange().getValues();  
  var sheet3 = ss.getSheetByName("Sheet3").getDataRange().getValues(); 


  // iterate mainsheet and check in sheet2 & sheet3 if duplicate values exist
  var nsheet2 = [];
  var nsheet3 = [];
  var mainsheetCol1 = [];// data in column1 of main sheet
  var mainsheetCol2 = [];// data in column2 of main sheet

  for(var n in mainsheet){
    mainsheetCol1.push(mainsheet[n][0]); //column1
    mainsheetCol2.push(mainsheet[n][3]); //column2
  }
  for(var n in sheet2){ // iterate sheet2 and test col 1 vs col 1 and co2 1 vs co2 1in sheet2
    var noDup1 = checkForDup(sheet2[n],mainsheetCol1,mainsheetCol2)
    if(noDup1){nsheet2.push(noDup1)};// if not present in sheet3 then keep
  } 
  for(var n in sheet3){  // iterate sheet3 and test col 1 vs col 1 and co2 1 vs co2  in sheet3
    var noDup2 = checkForDup(sheet3[n],mainsheetCol1,mainsheetCol2)
    if(noDup2){nsheet3.push(noDup2)};// if not present in sheet3 then keep
  }
  // view result
  Logger.log(nsheet2);
  Logger.log(nsheet3);

  // clear and update sheets
  ss.getSheetByName("Sheet2").getDataRange().clear();
  ss.getSheetByName("Sheet3").getDataRange().clear();
  ss.getSheetByName("Sheet2").getRange(1,1,nsheet2.length,nsheet2[0].length).setValues(nsheet2);
  ss.getSheetByName("Sheet3").getRange(1,1,nsheet3.length,nsheet3[0].length).setValues(nsheet3);
}

//Here can't seem to make it work to check if column 2 is not equal to the other sheets
//item is sheet2[n]
// s is mainsheetCol1
// s2 is mainsheetCol2
function checkForDup(item,s,s2){
  Logger.log(s+' = '+item[0]+'  ?')
  Logger.log(s2+' = '+item[1]+'  ?')
    if((s.indexOf(item[0])>-1) && (s2.indexOf(item[1])>-1))){
      return null;
    }
  return item;
}

希望有人可以帮助/指导我。谢谢!

2 个答案:

答案 0 :(得分:0)

试试这个:

function removeDuplicate(){

      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var mainsheet = ss.getSheetByName("Sheet1");
      var sheet2 = ss.getSheetByName("Sheet2");
      var sheet3 = ss.getSheetByName("Sheet3");
      var masterData = mainsheet.getDataRange().getValues(); 

      var sheetsToCheck = [sheet2,sheet3];  

      for(var i in sheetsToCheck){
        var valuesToCheck = sheetsToCheck[i].getDataRange().getValues();
        for(var j=0;j<valuesToCheck.length;j++){
          for(var k in masterData){
            if(masterData[k][0] == valuesToCheck[j][0] && !(masterData[k][1] == valuesToCheck[j][1])){
              sheetsToCheck[i].deleteRow(j+1);
            }
          }
        }
      }

    }

答案 1 :(得分:0)

Post处的代码无效。我已经修改了一点以使它工作。 在这里,

function removeDupsInOtherSheets() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s1 = ss.getSheetByName("Sheet1").getDataRange().getValues();  
  var s2 = ss.getSheetByName("Sheet2").getDataRange().getValues(); 
  var s3 = ss.getSheetByName("Sheet3").getDataRange().getValues();  
  // iterate s3 and check in s1 & s2 if duplicate values exist
  var nS1 = [];
  var nS2 = [];
  var s3Col1 = [];// data in column1 of sheet3
  for(var n=0; n<s3.length; ++n){ 
    s3Col1.push(s3[n][0]);
  }
  for(var n=0; n<s1.length; ++n){ // iterate sheet1 and test col 1 vs col 1 in sheet3
   var noDup1 = checkForDup(s1[n],s3Col1)
    if(noDup1){nS1.push(noDup1)};// if not present in sheet3 then keep
  } 
  for(var n=0; n<s2.length; ++n){  // iterate sheet2 and test col 1 vs col 1 in sheet3
    var noDup2 = checkForDup(s2[n],s3Col1)
    if(noDup2){nS2.push(noDup2)};// if not present in sheet3 then keep
  }
  Logger.log(nS1);// view result
  Logger.log(nS2);
  ss.getSheetByName("Sheet1").getDataRange().clear();// clear and update sheets
  ss.getSheetByName("Sheet2").getDataRange().clear();
  var nS1Length = nS1.length;
  var nS2Length = nS2.length;

 // This is the change needed in code you have copied from,
 **if(nS1 != undefined){
    data = []
    data[0] = nS1;
    var range = ss.getSheetByName("Sheet1").getRange(1,1);
    range.setValues(data);
 }
 if(nS2 != undefined){
    data = []
    data[0] = nS1;
    var range = ss.getSheetByName("Sheet2").getRange(1,1);
    range.setValues(data);
 }**
}

function checkForDup(item,s){
   Logger.log(s+' = '+item[0]+'  ?')
   if(s.indexOf(item[0])>-1){
     return null;
   }
   return item;
}