Remove rows based on duplicates in a single column in Google Sheets

后端 未结 3 1358
春和景丽
春和景丽 2021-01-23 00:30

I have spreadsheet similar to this:

I\'d like to remove all duplicates of row based on the first column data.

So from this screenshot row, 1 and 2 woul

相关标签:
3条回答
  • 2021-01-23 00:33

    I would...

    1. Create a new sheet
    2. For column A, do =Unique(Sheet1!A:A)
    3. Simply use VLOOKUP to populate the other columns. This will deliver the first value associated with the duplicates.
    0 讨论(0)
  • 2021-01-23 00:35

    You can also use a google apps script to do this. To open the script editor from google sheets:

    1. Choose the menu Tools > Script Editor.

    2. Copy and paste the following script:

      function removeDuplicates() {
          var sheet = SpreadsheetApp.getActiveSheet();
      
          var data = sheet.getDataRange().getValues();
          var newData = [];
          var ids = [];
          for (var i in data) {
            var row = data[i];
            var duplicate = false;
            if (ids.indexOf(row[0]) > -1) {
              duplicate = true;
            } else {
              duplicate = false;
              ids.push(row[0]);
            }
            if (!duplicate) {
              newData.push(row);
            }
        }
        sheet.clearContents();
        sheet.getRange(1, 1, newData.length, newData[0].length).setValues(newData);
        }
      

    This assumes that you want to dedupe based on the contents of the first row of you sheet. If not you can adjust the row references from the 0 index to any other index you wish.

    0 讨论(0)
  • 2021-01-23 00:47

    Maya's answer and the solution AJPerez linked both work.

    You can also use Filter View, which doesn't require deleting rows or creating new rows/sheets.

    First create a helper column, say to the left of all your data. If your data starts on row 1, then create a blank row above all your data; if not, you are fine. Afterwards, on the first row where you have data, say row 2, write in the formula

    =iserror(match(B2,B$1:B1,0))
    

    Replace "2" with the row number of the first row of your data and "1" with that number minus 1. Also populate the rest of the column with the formula. (Unfortunately, arrayformula doesn't work here.) The formula outputs TRUE when the entry in B# has not occurred in cells above.

    Note that this assumes your data now starts with column B and column B is where you want the filter to base. If that's not the case, just edit the column index too accordingly.

    Select this new helper column. Go to Data -> Filter Views... -> Create a new Filter. Select filter by value and check TRUE only.

    Caveat: this filter can only work if there are actually rows with TRUE value. This will always be the case as some entries are always unique.

    If you want to avoid the caveat in your future applications, you can use filter by conditions with custom formula. The formula b2 should work.

    But to begin with, even without the helper column, the above formula should work. Why doesn't it? That would be a good question for Google Support should it exist.

    0 讨论(0)
提交回复
热议问题