【发布时间】:2019-10-15 23:50:42
【问题描述】:
我目前有一列名为 JobID 的数据。在此列中,有来自每天运行的导入的重复项,并获取有关 JobID 的最新数据并将它们附加到工作表的顶部。
因此,最近的 JobID 行就是我们需要的数据行。
我想知道是否有一个脚本可以在名为“History”的工作表上运行以查找 JobID 列,在下面的每一行中搜索重复项并将它们删除,留下最上面、最近的 JobID 行在工作表中。
我知道使用 Google 表格中的“删除重复项”工具删除重复项非常容易……但我很懒,我正在尝试尽可能多地自动化此过程。
我下面的脚本运行没有错误,但仍然没有做我需要它做的事情。想知道我哪里出错了:
function removeDuplicates() {
//Get current active Spreadsheet
var sheet = SpreadsheetApp.getActive();
var history = sheet.getSheetByName("History");
//Get all values from the spreadsheet's rows
var data = history.getDataRange().getValues();
//Create an array for non-duplicates
var newData = [];
//Iterate through a row's cells
for (var i in data) {
var row = data[i];
var duplicate = false;
for (var j in newData) {
if (row.join() == newData[j].join()) {
duplicate = true;
}
}
//If not a duplicate, put in newData array
if (!duplicate) {
newData.push(row);
}
}
//Delete the old Sheet and insert the newData array
history.clearContents();
history.getRange(1, 1, newData.length, newData[0].length).setValues(newData);
}
【问题讨论】:
-
请分享电子表格。
-
嘿库珀!感谢您有兴趣在这里为我提供帮助。为了帮助我,这里是我正在处理的工作表的链接:link
-
原来我真正需要的只是“JobID”标题。
标签: csv google-apps-script google-sheets google-sheets-api google-sheets-formula