Last active
December 15, 2016 12:11
-
-
Save agryson/5517457 to your computer and use it in GitHub Desktop.
A Google App Script to notify someone specific when a file has been added to a folder.
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
/** | |
* Attribution: Alexander Gryson (agryson.net) | |
* This script stores a listing of the files in a folder. | |
* If a new item is placed in the folder, an email will be sent. | |
* LIMITATION: This script can only handle about 70 listings due to the 9kb limit for properties. | |
* If the number of listings goes above 50, a warning will be sent. | |
*/ | |
function listFilesInFolder() { | |
var folderID = '1234567890AZERTYUIOPQSDFGHJKLM_EXAMPLE'; //ID of the folder to watch | |
var admin = 'admin_example@gmail.com'; //Email of the person to notify | |
var items = DocsList.getFolderById(folderID).getFiles(); | |
var contents = []; | |
var roster = JSON.parse(ScriptProperties.getProperty('store' + folderID)); | |
var contains = function(arrayIn, value){ | |
if(arrayIn instanceof Array){ | |
for(var i = 0; i < arrayIn.length; i++){ | |
if(arrayIn[i] === value){ | |
return true; | |
} | |
} | |
} | |
return false; | |
}; | |
var notify = function(name, author, link){ | |
var subject = 'New draft for review'; | |
var body = 'There\'s a new draft for review titled "' + name + | |
'" from ' + author + | |
'. View the file here: ' + link; | |
GmailApp.sendEmail(admin,subject,body); | |
} | |
//Here's where the magic happens | |
for (var i = 0; i < items.length; i++) { | |
//Use IDs so that name changes don't break everything | |
var value = items[i].getId(); | |
contents.push(value); | |
if(!contains(roster, value)){ | |
notify(items[i].getName(), items[i].getOwner(), items[i].getUrl()); | |
} | |
} | |
//Store our new roster | |
ScriptProperties.setProperty('store' + folderID, JSON.stringify(contents)); | |
if(contents.length >= 50){ | |
var warnSubject = 'Roster nearly full.'; | |
var warnBody = | |
'Notifications only work if there are less than 70 items in the folder. The folder called ' | |
+ DocsList.getFolderById(folderID).getName() + | |
' has at least 50 items and will soon start producing errors. View the folder here: ' | |
+ DocsList.getFolderById(folderID).getUrl() + | |
'. If you really need more than 70 items in the same folder, please let Alex (' + admin + | |
') know so he can write a more robust script'; | |
GmailApp.sendEmail(admin,warnSubject,warnBody); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment