Skip to content

Instantly share code, notes, and snippets.

@samos123
Created October 20, 2018 05:05
Show Gist options
  • Save samos123/7f13560ac9bc682b841092e771da3171 to your computer and use it in GitHub Desktop.
Save samos123/7f13560ac9bc682b841092e771da3171 to your computer and use it in GitHub Desktop.
function prune_old_rows() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Form Submissions');
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
// assumes that there is a column "Added" as the first column
var addedColumn = 0;
var currentDate = new Date();
var twentyOneDaysAgo = new Date();
twentyOneDaysAgo.setDate(currentDate.getDate() - 21);
var rowsDeleted = 0;
for (var i = 0; i < numRows; i++){
var added = values[i][addedColumn];
if (typeof added.getMonth === 'function' && added < twentyOneDaysAgo) {
Logger.log("Deleting row " + i + ".")
sheet.deleteRow((parseInt(i) + 1) - rowsDeleted);
rowsDeleted++;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment