0

表 1

Timestamp           |Name | Shift.....| Date....| Letter..|    
19/05/2016 15:44:46|Mark | Swap      |  02/AUG | S       |    
Timestamp2           |Ivon | Give away | 25/ AUG | G       |    
Timestamp3           |Laura| Take      | 01/AUG  | A       |     
Timestamp4           |Ann  | Take      | 02/AUG  | A       |    
Timestamp5           |Ann  | Give away | 03/AUG  | G       |
Timestamp6           |Laura| Delete    | 01/AUG  | D       | 

由于这是一个谷歌表,我正在尝试运行以下脚本来删除与名称和日期列匹配的行。EG Laura 在 01AUG 的同一日期有 2 个条目。因此,我希望删除两行。我正在尝试以下脚本,但它不起作用。

    function removeDuplicates() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Form responses 2");
 var data = sheet.getDataRange().getValues();
  var newData = new Array();
  for(i in data){
    var row = data[i];
    var duplicate = false;
    for(j in newData){
      if( row[0] == newData[j][1] && row[1] == newData[j][3]){
  duplicate = true;
      }
    }
    if(!duplicate){
      newData.push(row);
    }
  }
  sheet.clearContents();
  sheet.getRange(1, 1, newData.length, newData[0].length).setValues(newData);
 }
4

1 回答 1

0

v4。决赛(我认为)。

如果 Cx 是“删除”或“删除”,则删除行 x 和 B 和 D 与行 x 相同的行。

function objTst(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Form responses 2");

  var megarng = sheet.getRange(2, 2, sheet.getLastRow(),sheet.getLastColumn()).getValues(); //[][]

  var rodel = [0]; //rows to delete

  for (i = 0; i<megarng.length; i++){
    if (String(megarng[i][1])==("Delete"||"delete")){ //megarng[][x] 0=b, 1=c, 2=d
      rodel.push(i+2) 

      for (g=0; g<megarng.length ; g++){

        if(String(megarng[g][0])===String(megarng[i][0]) && String(megarng[g][2])===String(megarng[i][2])){

          rodel.push(g+2);

          Logger.log(megarng[g][0]);
          Logger.log(megarng[i][0]);
          Logger.log(megarng[g][2]);
          Logger.log(megarng[i][2]);

          Logger.log(i+"i");
          Logger.log(g);
        }
      }
    }
    rodel.sort(function comp(a,b){return a-b});
    Logger.log(rodel);
  }
  for (i=rodel.length-1 ; 0<i; i--){if (rodel[i-1]!==rodel[i]){sheet.deleteRow(rodel[i]);}}
}
于 2016-06-04T06:04:50.597 回答