2014-02-17 109 views
0

我有一個包含重複行的電子表格。所有重複的行都需要刪除。Google電子表格查找重複項並刪除全部

例子:

Apple  fruit 

發現這個腳本...也許一個好的點開始:

Superman superhero 
Batman  superhero 
Apple  fruit 
Superman superhero 
Batman  superhero 

運行腳本我只希望以後?

function removeDuplicateRows() { 
    var sheet = SpreadsheetApp.getActiveSheet(); 
    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); 
    } 
    } 
    sheet.clearContents(); 
    sheet.getRange(1, 1, newData.length, newData[0].length).setValues(newData); 
} 
+0

你有沒有試過自己的腳本?也許你也可以添加google-apps-script標籤。 –

+0

是的,我做了,它刪除重複,但只有一個,並保留一個。我需要將它們都刪除。 – Kortschot

+0

最新的預期結果是什麼?刪除所有重複項或刪除同一列中的重複項(意味着在不同列中仍可能存在2個具有相同內容的單元格?)存在數據的單元格應該與遇到的第一個發生位置保持相同的位置,或者可以重新排列工作表的頂部。 – Harold

回答

0

這是對另一個論壇上this post提出的稍微不同的算法的修改。需要注意的是,它會對數據進行排序以及刪除重複的行(但可以解決這個問題)。

function removeDuplicatesAndSort() 
{ 
    var headerRow = false; 

    var sheet = SpreadsheetApp.getActiveSheet(); 
    var data = sheet.getDataRange().getValues(); 

    if (headerRow) var header = data.splice(0, 1); 
    data.sort(); 
    var counter = 0, counter2, row; 
    while (counter < (data.length - 1)) 
    { 
    row = data[counter].join(","); 
    if (row == data[counter + 1].join(",")) //stuff be deleted 
    { 
     counter2 = 2; 
     while ((counter + counter2) < data.length && row == data[counter + counter2].join(",")) 
     { 
     counter2++; 
     } 
     data.splice(counter, counter2); 
    } 
    else //move to next row 
    { 
     counter++; 
    } 
    } 
    if (headerRow) data.unshift(header[0]); 
    sheet.clearContents(); 
    sheet.getRange(1, 1, data.length, data[0].length).setValues(data); 
} 
+0

是的,這似乎是正常工作!謝謝! – Kortschot

0

的問題是,當前行儘快加入到newData它達到(如果它已不存在),因此總是被添加一個副本。 我編輯了第二個循環來檢查當前行是否存在於工作表中的其他任何地方,然後將其添加到newData

該解決方案不會排序數據,但移動剩餘行至表的頂部。如果你想讓他們留在原地,請看Harold's answer

我寫了一些評論來幫助你理解。

function removeDuplicates() { 
    var sheet = SpreadsheetApp.getActiveSheet(); 
    var data = sheet.getDataRange().getValues(); 

    //Get the number of the last row with data 
    var lastRow = sheet.getLastRow(); 
    var lastColumn = sheet.getLastColumn(); 
    var newData = new Array(); 
    for(i in data){ 

    //Our current row 
    var row = data[i]; 

    var duplicate = 0; 

    //A loop with going through all rows with data 
    for(var j = 1; j <= lastRow; j++){ 

     //A variable with the joined data of the row to compare with our current row 
     var currValue = sheet.getRange(j, 1, 1, lastColumn).getValues().join(); 
     if(row.join() == currValue){ 

     //If an occurrence of the current row exists, add 1 to the variable "duplicate" 
     duplicate++; 
     } 
    } 

    //The second loop will find the original row, so the duplicate variable will always 
    //be at least 1, if any duplicates exist, it will be more and the row won't be pushed 
    if(duplicate == 1){ 
     newData.push(row); 
    } 
    } 
    sheet.clearContents(); 
    sheet.getRange(1, 1, newData.length, newData[0].length).setValues(newData); 
} 
0

不知道這是你想要的,但在這裏一個小腳本,將做一個非常具體的行動:通過柱(不能跨其他列)刪除所有重複和保持,他們是不重複的值。

function eradicateDuplicate(){ 
    var sheet = SpreadsheetApp.getActiveSheet(); 
    var data = sheet.getDataRange().getValues(); 
    var newData = new Array(); 
    var cols = data[0].length; 
    var objTable = {}; 
    for(var j in data[0]){ 
    objTable[j]=new Object(); 
    } 
    for(var i in data){ 
    for(var j in data[i]){ 
     if(typeof objTable[j][data[i][j]]=="undefined"){ 
     objTable[j][data[i][j]]=1; 
     } 
     else{ 
     objTable[j][data[i][j]]+=1; 
     } 
    } 
    } 
    for(var i in data){ 
    for(var j in data[i]){ 
     if(objTable[j][data[i][j]]>1)data[i][j]=""; 
    } 
    } 
    sheet.getDataRange().setValues(data); 
}