Skip to content

Instantly share code, notes, and snippets.

@bignimbus
Created March 25, 2014 12:15
Show Gist options
  • Save bignimbus/9760702 to your computer and use it in GitHub Desktop.
Save bignimbus/9760702 to your computer and use it in GitHub Desktop.
I created a Google Sheet to organize short bursts of lesson plan content across grade and content levels at Dever School. Script automatically deletes out-of-date entries. Script will also sort the page after deleting duplicates so that up-to-date information appears first.
/*
I created a Google Sheet to organize short bursts of lesson plan content across grade and content levels at Dever School.
Script automatically deletes out-of-date entries.
Script will also sort the page after deleting duplicates so that up-to-date information appears first.
*/
function removeDuplicates(){
Logger.clear();
Logger.log("starting over...");
var ss = SpreadsheetApp.openById(**ID NUMBER HERE **); //get spreadsheet
var sheet = ss.getSheets()[0]; //specify sheet
var lastRow = sheet.getLastRow(); //find last row
var newD = sheet.getRange(1, 1, lastRow, 1).getValues().toString(); //import two copies of 1st to check email against one another
var oldD = sheet.getRange(1, 1, lastRow, 1).getValues().toString();
var newData = newD.split(","); //split into arrays
var oldData = oldD.split(",");
for(i=lastRow; i>0; i--){ //start at bottom of sheet (newest entries, keep this data)
for(j=0; j<lastRow; j++){ //cross-check from top of sheet (oldest entries, delete this data)
var jK=j+1; //match array # (starts w/ 0) to row # in spreadsheet (starts w/ 1)
if(newData[i] == oldData[j] && i!==j){
//if emails match, delete old row and start over
//this resets the lastRow and for loops to adjust for the deleted data
Logger.log(oldData[j] + " in row " + jK + " deleted.*************");
sheet.deleteRow(jK);
lastRow = sheet.getLastRow();
newD = sheet.getRange(1, 1, lastRow, 1).getValues().toString();
oldD = sheet.getRange(1, 1, lastRow, 1).getValues().toString();
newData = newD.split(",");
oldData = oldD.split(",");
i=lastRow;
j=0;
} // if
Logger.log("Old row " + jK + " completed.");
} // for j
Logger.log("Done with new row " + i);
} // for i
var range = sheet.getRange('A2:C100');
range.sort({column: 3, ascending: false}); //sort data so that newest is on top
Logger.log("sheet sorted!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment