删除Google表格电子表格中的重复项

时间:2018-05-02 11:45:39

标签: javascript google-apps-script

我的电子表格中包含一些数据:

Staff No. - First Name - Last Name - Age
12345     - John       - Doe       - 29
12345     - John       - Doe       - 30
12456     - Jane       - Doe       - 29
12345     - John       - Doe       - 29

我找到了一些允许我删除重复行的代码。 使用上面的"电子表格数据"举个例子,我可以删除第4个条目没问题。不幸的是,这不是用户正在寻找的东西。

我需要调整以下代码来搜索第一列(而不是整行)中的值,如果找到匹配项,则将该行添加到临时数组中。

换句话说,使用上面的"电子表格数据"作为一个例子,我需要只有John Doe的第一个记录,29岁回到阵列。 John Doe的其他2个条目需要被丢弃。 (同时仍保留Jane Doe的条目。)

守则:

function onOpen() {
    var ui = SpreadsheetApp.getUi();
    ui.createMenu('Duplicate Removal Tool')
        .addSubMenu(ui.createMenu('Commands').addItem('Remove Duplicates', 'removeDuplicates')).addToUi();
}

function removeDuplicates() {
    var ss = SpreadsheetApp.getActive() // Gets the Spreadsheet where the code is located in.
    var sheet = ss.getActiveSheet(); // Gets the Sheet the user is currently focused on.
    var data = sheet.getDataRange().getValues(); // Gets the values of the above sheet.
    var newData = new Array(); // Creates a temporary blank array.

    for (i in data) {

        var row = data[i] // Row Variable equals element of array for current iteration of loop.
        var duplicate = false; // Duplicate variable set to default on each iteration of outer loop.

        for (j in newData) {

            if (row.join() == newData[j].join()) { //Converts elements of the arrays to string and compare values.
                duplicate = true;
            }
        }
        if (!duplicate) {
            newData.push(row); // If rows do not match add current element in row array to newData array.
        }
    }


    sheet.clearContents(); // Clear the contents of the sheet
    sheet.getRange(1, 1, newData.length, newData[0].length) 
        .setValues(newData); // Write values of newData array to sheet.
}

尝试编号2:     这是我的第二次尝试。适用于大多数情况。抛出一行是超出界限的错误。没问题,这很容易解决。这个版本的问题是它在50-100条记录上运行良好。最多可以获得500多条记录,执行时间可以通过:

function removeDuplicates() {
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getActiveSheet();
  var newData = []

  var i, j, outerLoopRow, innerLoopRow, lastRow;

  lastRow = sheet.getLastRow();

  for (i=1; i < lastRow; i++) {
    outerLoopRow = sheet.getRange(i, 1, 1, 1).getValue();

    if (i == lastRow) break


    for (j= i +1; j < lastRow+1; j++) {
      innerLoopRow = sheet.getRange(j,1,1,1).getValue()
      if (outerLoopRow == innerLoopRow){
        ss.deleteRow(j)
      }

   } // End of Inner Loop
  } // End of OuterLoop

} //End of Function

1 个答案:

答案 0 :(得分:0)

以下是我用来获得所需结果的代码。 关于它如何工作的解释在评论中。 如果有人看到这个需要一些帮助,请随时告诉我,我会尽我所能来帮助我。

//Add Menu to current Sheet
function onOpen(){
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Duplicate Removal Tool')
    .addSubMenu(ui.createMenu('Commands').addItem('Remove Duplicates', 'removeDuplicates')).addToUi();
}

function removeDuplicates() {
  // Insert Spreadsheet ID below
  // Insert name of Sheet in spreadsheet below
  var ss = SpreadsheetApp.openById("SPREADSHEET ID HERE");
  var sheet = ss.getSheetByName("SHEET NAME HERE");

  //Create Variables
  var array1, array2, array3, lastRow, i, j, k, rowToDelete, reverseArray;

  lastRow = sheet.getLastRow(); // Get last row in sheet with data
  array1 = sheet.getRange(2, 1, lastRow, 1).getValues(); // Get all values in column A
  array2 = array1 // Copy array1 (All values in A)
  array3 = [] // Create a blank array

  // Outer Loop - Perform Inner Loop for each value in array 1
  // Inner loop, compare first value against all other values in descending order.
  // If match is found, check if value already exists in array 3
  // If value does not exist, push the row number to array 3
  for (i = 0; i < array1.length; i++) {
    for (j = i + 1; j < array2.length; j++) {
      if (array1[i].join() == array2[j].join()) {
        if(array3.indexOf(j + 2) == -1)
          array3.push(j + 2)
      }
    } // End of Inner Loop
  } // End of Outer Loop

  //Sort Array 3 in numerical order.
  // Reverse array so highest value is first and lowest value is last
  // For each value in array 3, delete the row with corresponding number in array 3
  reverseArray = array3.sort(function (a, b) {  return a - b;  }).reverse()
  for (k = 0; k < array3.length; k++) {
    rowToDelete = reverseArray[k];
    ss.deleteRow(rowToDelete);
  }
}// End of Function