Skip to content

Instantly share code, notes, and snippets.

@rapartipoorna
Created August 6, 2021 04:22
Show Gist options
  • Select an option

  • Save rapartipoorna/4de8f95ae46099ec41bbc515a9f2c001 to your computer and use it in GitHub Desktop.

Select an option

Save rapartipoorna/4de8f95ae46099ec41bbc515a9f2c001 to your computer and use it in GitHub Desktop.
[ Apps script ] it will remove all duplicates from each sheet in Active spreadsheet. script should be bounded to spreadsheet to work properly
//Removes duplicate rows from the current sheet.
function removeDuplicates() {
//Get current active Spreadsheet
// var sheet = SpreadsheetApp.getActiveSheet();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets()
for(var s=0;s<sheets.length;s++){
//Get all values from the spreadsheet's rows
var data = sheets[s].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
sheets[s].clearContents();
sheets[s].getRange(1, 1, newData.length, newData[0].length).setValues(newData);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment