下面是一个 JavaScript 脚本示例,它可以去除指定 Google Sheet 中的重复行,并且可以删除该表格。
function deleteDuplicates() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
// Create a new array to store unique rows
var uniqueRows = [];
var dups = 0; // Keep track of the number of duplicates
// Loop through each row
for (var i = 0; i < numRows; i++) {
var row = values[i];
// Check if the row is unique
if (uniqueRows.indexOf(row.join()) == -1) {
uniqueRows.push(row.join());
} else {
// If the row is not unique, delete it
sheet.deleteRow(i + 1 - dups);
dups++;
}
}
// If there were duplicates, show a message to the user
if (dups > 0) {
Browser.msgBox("Deleted " + dups + " duplicate rows.");
// If there are no more rows left, delete the sheet
if (numRows == dups) {
ss.deleteActiveSheet();
}
} else {
Browser.msgBox("No duplicate rows found.");
}
}
要使用此脚本,请在 Google Sheet 中打开 Script 编辑器,并将其粘贴到脚本编辑器中。您可以使用“即时执行”(菜单栏中的“运行”按钮)来运行此脚本,或者为其创建一个自定义菜单或触发器。