Created
October 20, 2018 05:05
-
-
Save samos123/7f13560ac9bc682b841092e771da3171 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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