Advertisement
Guest User

Folder Notification

a guest
Nov 20th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. FOLDER_ID = "0Bz-U3E0jxSujdENGdWVpeHRNMkk" //This is the folder ID
  2. SPREADSHEET_ID = "13gES4Jgu1TCdbYZ4kR3f0rtuNZKNlGvL4TZ7ieVjJWI" //This is the spreadsheet ID
  3.  
  4. SHEET_FILES_NAME = "Files"
  5. SHEET_EMAIL_NAME = "Emails"
  6.  
  7. EMAIL_TEXT_HTML = "<p> Hi %name%, <br><br>\
  8. %filename% has been added to the minutes folder. It can be accessed <a href=\"%filelink%\">here</a>. <br><br>\
  9. If previous minutes are needed, you can access them <a href=\"%folderlink%\">here</a>.</p>"
  10.  
  11. EMAIL_TEXT_RAW = "Hi %name%, \n %filename% has been added to the minutes folder. It can be accessed at %filelink% \n If previous minutes are needed, you can access them at %folderlink%"
  12.  
  13. function checkFiles() {
  14.   fileTableReset() //Remove all of the "y"s in the "exists" folder, so they can be re-added
  15.  
  16.   var folder = DriveApp.getFolderById(FOLDER_ID) //Get the folder name and the files inside it
  17.   var files = folder.getFiles()
  18.  
  19.   var spreadsheet = SpreadsheetApp.openById(SPREADSHEET_ID)
  20.   var sheetFiles = spreadsheet.getSheetByName(SHEET_FILES_NAME) //Open the spreadsheet's sheet show
  21.  
  22.   while (files.hasNext()) {
  23.     var file = files.next() //Go to the next file
  24.    
  25.     var fileIDRow = fileIdAlreadyExists(file.getId()) //Get the row no. where the file is held
  26.    
  27.     if (fileIDRow == -1) //If the file isn't in the spreadsheet
  28.     {
  29.       Logger.log("Not found in sheet: File ID "+file.getId()+", Sending email")
  30.       sendNewFileEmail(file, folder) //Send email out
  31.       sheetFiles.appendRow([file.getId(), "y"]) //Add it to the spreadsheet
  32.     }
  33.     else
  34.     {
  35.       Logger.log("Found: File Name "+file.getName()+", Not sending email")
  36.       sheetFiles.getRange(fileIDRow, 2).setValue("y") //mark the ID as existing
  37.     }
  38.   }
  39.  
  40.   fileTableTidy() //Tidy up any deleted files (ones without "y"s next to the id.
  41. }
  42.  
  43. function fileTableTidy() { //Tidy up any deleted files (ones without "y"s next to the id.
  44.  
  45.   var spreadsheet = SpreadsheetApp.openById(SPREADSHEET_ID)
  46.   var sheetFiles = spreadsheet.getSheetByName(SHEET_FILES_NAME) //Open files sheet
  47.  
  48.   columns = sheetFiles.getRange(2,2,sheetFiles.getLastRow()-2).getValues()//Get all of the files stored
  49.  
  50.   for (var i = 0; i < columns.length; i++) {
  51.     if (columns[i] != "y") { //If the row doesn't have a y (if it doesn't exist)
  52.       sheetFiles.deleteRow(i+2) //Delete the row
  53.     }
  54.   }
  55. }
  56.  
  57. function sendNewFileEmail(file, parentFolder) { //Send the notification email to the group
  58.  
  59.   Logger.log("Send email for file " + file.getName())
  60.  
  61.   var spreadsheet = SpreadsheetApp.openById(SPREADSHEET_ID)
  62.   var sheetEmails = spreadsheet.getSheetByName(SHEET_EMAIL_NAME) //Open the emails spreadsheet
  63.  
  64.   data = sheetEmails.getRange(2,1,sheetEmails.getLastRow()-1,2).getValues() //Download a 2-dimension array of all of the emails and preferred names
  65.  
  66.   for (var i = 0; i < data.length; i++) {
  67.    
  68.     var bodyHtml = EMAIL_TEXT_HTML //Get email template
  69.    
  70.     bodyHtml = bodyHtml.replace("%name%", escapeHtml(data[i][1]))
  71.     bodyHtml = bodyHtml.replace("%filename%", escapeHtml(file.getName()))
  72.     bodyHtml = bodyHtml.replace("%filelink%", escapeHtml(file.getUrl()))
  73.     bodyHtml = bodyHtml.replace("%folderlink%", escapeHtml(parentFolder.getUrl())) //Replace the placeholders in the template
  74.    
  75.     var bodyRaw = EMAIL_TEXT_RAW //Get email template
  76.    
  77.     bodyRaw = bodyRaw.replace("%name%", data[i][1])
  78.     bodyRaw = bodyRaw.replace("%filename%", file.getName())
  79.     bodyRaw = bodyRaw.replace("%filelink%", file.getUrl())
  80.     bodyRaw = bodyRaw.replace("%folderlink%", parentFolder.getUrl()) //Replace the placeholders in the template
  81.    
  82.     MailApp.sendEmail(data[i][0], "New Minutes: " + file.getName() , bodyRaw, {htmlBody: bodyHtml}) //Send the email
  83.   }
  84. }
  85.  
  86. function fileTableReset() { //Gets rid of all of the "y"s in the "Exists?" Column.
  87.  
  88.   var sheet = SpreadsheetApp.openById(SPREADSHEET_ID).getSheetByName(SHEET_FILES_NAME)
  89.  
  90.   sheet.getRange("B2:B").clear() //Clear all of the cells in that area (where the "y"s are.
  91. }
  92.  
  93. function fileIdAlreadyExists(fileId) { //Check if file exists in spreadsheet and if so, where it is
  94.   var spreadsheet = SpreadsheetApp.openById(SPREADSHEET_ID)
  95.   var sheetFiles = spreadsheet.getSheetByName(SHEET_FILES_NAME) //open the spreadsheet
  96.  
  97.   columns = sheetFiles.getRange(2,1,sheetFiles.getLastRow()).getValues() //get a 1D array of the file IDs
  98.  
  99.   for (var i = 0; i < columns.length; i++) {
  100.     if (columns[i] == fileId) {
  101.       return i+2 //if it is there, return the row number (+1 for the top row, +1 for converting from the 0 index of array to 1 index of spreadsheet)
  102.     }
  103.   }
  104.   return -1 //If not there, return -1
  105. }
  106.  
  107. function escapeHtml(unsafe) {
  108.     return unsafe
  109.          .replace(/&/g, "&amp;")
  110.          .replace(/</g, "&lt;")
  111.          .replace(/>/g, "&gt;")
  112.          .replace(/"/g, "&quot;")
  113.          .replace(/'/g, "&#039;");
  114.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement