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

后端 未结 3 1359
春和景丽
春和景丽 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: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.

提交回复
热议问题