更新时间:2023-01-17 18:36:54
我会尝试
function removeDuplicates()
{
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("test");
var dataRange = sheet.getDataRange();
var data = dataRange.getValues();
var indexOfStatusColumn = 4;
var newData = [];
data.forEach(function (row, rowI){
var isDuplicate = false
if (newData.length)
newData.forEach(function (dup, dupI)
{
if (dup[3] == row[3])
{
if (row[indexOfStatusColumn] == "Complete" && dup[indexOfStatusColumn] != "Complete")
newData[dupI][indexOfStatusColumn] = "Complete";
isDuplicate = true;
return (false);
}
})
if (!isDuplicate)
newData.push(row);
});
dataRange = sheet.getRange(2, 1, dataRange.getLastRow() - 1, dataRange.getLastColumn());
dataRange.clearContent();
sheet.getRange(1, 1, newData.length, newData[0].length).setValues(newData);
}
几件事
forEach()
循环更有效false
来中断循环,以避免无用地解析所有newData
谷底forEach()
loop it's more efficientfalse
to avoid parsing trough all your newData
uselesslydata
上重复了Complete
,而不是在newData
上newData
中的值更改为"Complete"(完整)(请注意,如果两行中都有不同的数据,可能会扭曲数据).Complete
on data
and not on newData
if sonewData
to "Complete" this way it will keep the complete status (be careful if there's different datas on both rows it will probably twist datas).
clearContent()
以避免除去工作表的所有内容,而仅除去工作表的特定部分.在这里,我重建了它以保留标题
clearContent()
from a range to avoid removing all the content of the sheet but only a specific portion of it. Here I've rebuilded it to keep the header参考