Last active
June 11, 2018 07:11
-
-
Save braxtone/1eed2b4cff5861069c6dc545d4685e80 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
/*************************************************** | |
Script will send an email notification to you or other email addresses | |
when a file in a given Google folder has been added, or modified. | |
To create a copy, use https://docs.google.com/spreadsheets/d/13z6ExSxdhrAfjppSIrGw6yLvByRjTIA3iWWpi97dgsQ/edit?usp=sharing | |
***************************************************/ | |
function checkForChangedFiles() { | |
var config = loadConfiguration(); | |
var folderSearch = '"' + config["folderID"] + '" ' + 'in parents'; | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(config["sheetName"]); | |
var startTime = getLastUpdatedFileTime(config["sheetName"]); | |
Logger.log("Looking for files posted since: " + startTime) | |
var search = '(trashed = true or trashed = false) and '+ folderSearch +' and (modifiedDate > "' + startTime + '")'; | |
var files = DriveApp.searchFiles(search); | |
var row = "", count=0; | |
Logger.log("Found " + countFiles(files) + " files to report.") | |
// Didn't find a reset() method for unwinding the iteration, so we'll waste some GCP cycles. | |
var files = DriveApp.searchFiles(search); | |
while(files.hasNext()) { | |
var file = files.next(); | |
var fileName = file.getName(); | |
var fileURL = file.getUrl(); | |
var lastUpdated = Moment.moment(file.getLastUpdated()).toISOString(); | |
var dateCreated = Moment.moment(file.getDateCreated()).toISOString(); | |
row += "<li>" + lastUpdated + " <a href='" + fileURL + "'>" + fileName + "</a></li>"; | |
sheet.appendRow([dateCreated, lastUpdated, fileName, fileURL]); | |
count++; | |
} | |
if (row !== "") { | |
row = "<p>" + count + " file(s) changed. Here's the list:</p><ol>" + row + "</ol>"; | |
row += config["emailBody"]+"<br>" + "<br><small> "+config["emailFooter"]+" </a>.</small>"; | |
MailApp.sendEmail(config["emailFromAddr"], config["emailSubject"], "", {name: config["emailFromName"], htmlBody: row, cc: config["emailCC"]}); | |
} | |
} | |
function loadConfiguration() { | |
// Ideas on how to pull config from the spreadsheet | |
//var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
//var sheet = ss.getActiveSheet(); | |
//var email = sheet.getRange("E1").getValue(); | |
var config = { | |
"folderID": "11cpkbtpxWexample-example4pJGrYxApm", | |
"sheetName": "File Change Report", | |
"emailFromAddr": "[email protected]", | |
"emailFromName": "Bob Example", | |
"emailCC": "Other Dude <[email protected]>", | |
"emailSubject": "[FileUpdates] New file uploaded", | |
"emailBody": "New file has been uploaded, get to it!", | |
"emailFooter": "To stop these notifications, please contact [email protected]" | |
}; | |
Logger.log("### Configuration Start ###:"); | |
for( c in config) { | |
Logger.log(c + ": " + config[c]); | |
}; | |
Logger.log("### Configuration End ###:"); | |
return config; | |
}; | |
function countFiles(files) { | |
var count,file; | |
count = 0; | |
while (files.hasNext()) { | |
count++; | |
file = files.next(); | |
Logger.log(file.getName()); | |
}; | |
return count; | |
}; | |
function getLastUpdatedFileTime(sheetName) { | |
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName); | |
var dateModifiedRange = ss.getRange("B1:B"); | |
var dateModifieds = dateModifiedRange.getValues(); | |
var lastModifiedRowNum = dateModifieds.filter(String).length; | |
var lastModifiedTimestamp = dateModifieds[lastModifiedRowNum - 1][0] | |
Logger.log("Raw: " + lastModifiedTimestamp) | |
// To get access to Moment.js libraries, you need to add the library as a resource using the ID MHMchiX6c1bwSqGM1PZiW_PxhMjh3Sh48 | |
var lastModifiedTimestamp = Moment.moment(lastModifiedTimestamp).toISOString(); | |
Logger.log("Parsed(ISO8601): " + lastModifiedTimestamp); | |
return lastModifiedTimestamp | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment