Created
November 15, 2023 16:30
-
-
Save fawkesley/f3ad9c95fd4a7de1090e2313d53115e5 to your computer and use it in GitHub Desktop.
Google Sheets Apps Script to redact certain columns for rows older than 90 days (for use with Google Forms)
This file contains 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 scrubEmailsAndNamesOfGamesStartedOver90DaysAgo() { | |
const sheetName = 'responses'; | |
const ss = SpreadsheetApp.getActiveSpreadsheet(); | |
const sheet = ss.getSheetByName(sheetName); | |
if(!sheet) { | |
throw new Error('failed to get sheet `' + sheetName + '`'); | |
} | |
const data = sheet.getDataRange().getValues(); | |
const now = new Date(); | |
var sixMonthsAgo = new Date(); | |
sixMonthsAgo.setDate(now.getDate()-182); | |
for (var rowNumber = 2; rowNumber < 1+data.length; rowNumber++) { | |
var dateString = getByName(data, 'date', rowNumber); | |
if (dateString == '???' || dateString == '') { | |
Logger.log('ignoring row ' + rowNumber + ' with date column `' + dateString + '`'); | |
continue; | |
} | |
var gamePlayedDate = new Date(dateString); | |
// Logger.log('dateString: ' + dateString); | |
// Logger.log('gamePlayedDate: ' + gamePlayedDate); | |
if(isNaN(gamePlayedDate)) { | |
throw new Error('failed to parse date: `' + dateString + '`'); | |
} | |
Logger.log('checking row ' + rowNumber + ', played ' + gamePlayedDate); | |
if (gamePlayedDate < sixMonthsAgo) { | |
Logger.log('scrubbing row ' + rowNumber + ': ' + dateString + ' is >90 days ago'); | |
setByName(sheet, data, 'name', rowNumber, 'DELETED'); | |
setByName(sheet, data, 'email', rowNumber, 'DELETED'); | |
} | |
} | |
} | |
function getByName(data, colName, rowNumber) { | |
const colIndex = data[0].indexOf(colName); | |
if (colIndex == -1) { | |
throw new Error('failed to get column called `' + colName + '`'); | |
} | |
const rowIndex = rowNumber-1; | |
return data[rowIndex][colIndex]; | |
} | |
function setByName(sheet, data, colName, rowNumber, value) { | |
const colIndex = data[0].indexOf(colName); // colIndex is 0-indexed | |
if (colIndex == -1) { | |
throw new Error('failed to set column called `' + colName + '`'); | |
} | |
const colNumber = colIndex + 1; | |
sheet.getRange(rowNumber, colNumber).setValue(value); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment