我已将此tutorial引用以删除我的整个Google电子表格中的重复项,但是当工作表包含大量数据时需要很长时间。因此,我需要删除最后15个非空行中的重复项(仅保留一条记录)。
这是代码。
function removeDuplicates() {
var sheet = SpreadsheetApp.openById('11SbGOCZDiTc21Jjnh7r4XW43QNtdV2L0fGdzHDxRvdA');
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.join() == newData[j].join()){
duplicate = true;
}
}
if(!duplicate){
newData.push(row);
}
}
var first = sheet.getSheetByName("Sheet1");
first.clearContents();
first.getRange(1, 1, newData.length, newData[0].length).setValues(newData);
}
答案 0 :(得分:1)
根据要求进行修改,根据前两列比较重复项并列出A到E列:
function removeDuplicatesRev() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet =ss.getSheetByName("Sheet1")
var lr=sheet.getLastRow()
var lc=sheet.getLastColumn()
var data = sheet.getRange(2,1,lr,lc).getValues();//Changed to get all columns
var newData = new Array();
var alen=data.length
var adjlen=alen-15
for(i=adjlen;i<alen;i++){
var row = data[i];
var duplicate = false;
for(j in newData){
if(row[0] == newData[j][0] && row[1] == newData[j][1]){ //changed to compare col A&B
duplicate = true;
}
}
if(!duplicate){
newData.push(row);
}
}
var clr =sheet.getRange(adjlen+1, 1, 15, sheet.getLastColumn())
clr.clearContent()
sheet.getRange(adjlen+1, 1, newData.length, newData[0].length).setValues(newData);
}