删除重复行并将第一列添加到原始行 Google 脚本

Delete duplicate rows and add first column to original row Google script

我一直在尝试在 Internet 上寻找一段代码或方向来帮助我解决这个问题。
基本上,我有一组四列的数据,在最新一列中给出了数据所代表的描述。
我每天都在导入新数据,其中描述可能相同,但给出了一组不同的数据,它们只是数字。
现在我希望我的脚本找到重复项,获取数据并将其添加到原始数据并删除重复项。
所以,我只想累积第一列并删除其余数据。

我知道如何找到重复项、删除整个数据集以及将没有重复项的新数据集推送到 sheet。
但是,我在网上找不到任何可能性。可能有可能在 var Newdata 中获取一个范围的值,将其复制到找到重复项的行,然后将整个数组推送到该数组。
但是,我尝试合并的所有内容都给我带来了多个错误和无限的计算时间。

我希望有人能帮助我。

function DuplicateRemoval(){
  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);
}

是这样的吗?

function overwriteWithNew() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var data = sheet.getDataRange().getValues();
  var newData = [];

  data.forEach(function(row, rowI) {
    if (!newData.some(function(row2) {return row[0] === row2[0];})) { // If the key is not in the output yet
      for (var row2I = data.length - 1; row2I >= rowI; row2I--) {     // Then, starting from the last column
        if (data[row2I][0] === row[0]) {                              // Find the latest data with the same key
          newData.push(data[row2I]);                                  // Add it to the output
          break;                                                      // And continue with the next row
        }
      }
    }
  });
  sheet.clearContents();
  sheet.getRange(1, 1, newData.length, newData[0].length).setValues(newData);   
}