2

We have a remove duplicates function that removes rows that are previously duplicated, i.e., where Row 2, comprising B1 and B2, is deleted if B1 and B2 are identical to Row 1's A1 and A2. But we want it to be such that Row 2 is deleted only if B2 is identical to A2. Here is the code we have thus far:

    function removeDuplicates() {
  const startTime = new Date();
  const newData = [];
  const sheet = SpreadsheetApp.getActiveSheet();
  const data = sheet.getDataRange().getValues();
  const numRows = data.length;
  const seen = {};

  for (var i = 0, row, key; i < numRows && (row = data[i]); i++) {
    key = JSON.stringify(row);
    if (key in seen) {
      continue;
    }
    seen[key] = true;
    newData.push(row);
  };

The function should only affect latter rows and not former rows (i.e., Row 2 should be deleted, and not Row 1).

1 Answer 1

1

Maybe you can try a different approach like applying a formula on the spreadsheet like =UNIQUE(Sheet1!B:B) and copy the result to a new sheet. This should also be possible programmatically. See also:

EDIT: I think what you need is described in a Google Developer tutorial. https://developers.google.com/apps-script/articles/removing_duplicates See section "Variation". I have adapted this to match duplicates on the second column. I have tested this with a sample spreadsheet.

function removeDuplicates() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var data = sheet.getDataRange().getValues();
  var newData = [];
  for (var i in data) {
    var row = data[i];
    var duplicate = false;
    for (var j in newData) {
      if(row[1] == newData[j][1]){
        duplicate = true;
      }
    }
    if (!duplicate) {
      newData.push(row);
    }
  }
  sheet.clearContents();
  sheet.getRange(1, 1, newData.length, newData[0].length).setValues(newData);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, but the UNIQUE formula won't work for us because we need to retain congruence between columns. We would need to use that formula for two columns (A, which is for names, and B, which is for emails). But running the formula for A and B displaces the original matching (where A1 is the name of the person whose email is in B2).
The UNIQUE function is also a bit unwieldy for sheets to which we are adding new entries and want to check that each new entry is not a duplicate from preceding rows.
Ok, I see. Tried this myself in does not work as I thought. How about the first answer to remove-rows-based-on-duplicates-in-a-single-column-in-google-sheets? I tried this an it works for me, at least, when I insert the filter on the same spreadsheet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.