1
votes

I've set up a google drive folder structure for work with 2 levels of sub-folders. e.g.

Main folder

-Area 1

---Excluded area

-Area 2

---Excluded area

I need an email notification each time a file is modified to ensure other users have to most up to date data. I've found a script online which mostly works but I need it to be able to check within the sub-folders for modifications which this script doesn't do. Any help would be appreciated. Thanks, Amanda

function GoogleDriveModifiedFiles(){ 

// Folder ID "XXXXXXXxxXXXwwerr0RSQ2ZlZms" of the folder to monitor for changes
   var folderID = '"' + "0Bwz931NfDj3HUnhTVXBuc1lqMTg" + '"';

// Email Configuration  
var emailFromName ="Work Google Drive (Do Not Reply)";
var emailSubject = "Work Google Drive content has been modified";
var emailBody = "<br>You are receiving this email because a folder/file shared with you on Google Drive has been modified, click on the link to view new content<br>"
var emailFooter = "Any questions please contact [email protected]";

var folderSearch = folderID + " " + "in parents";
var ss = SpreadsheetApp.getActiveSpreadsheet(); 
var sheet = ss.getActiveSheet();
var email = sheet.getRange("E1").getValue();
var timezone = ss.getSpreadsheetTimeZone();
var today     = new Date();

var oneDayAgo = new Date(today.getTime() - 1 * 60 * 1000);  

var startTime = oneDayAgo.toISOString();

var search = '(trashed = true or trashed = false) and '+ folderSearch +' and (modifiedDate > "' + startTime + '")';   

var files  = DriveApp.searchFiles(search);

var row = "", count=0;

while( files.hasNext() ) {

var file = files.next();
var fileName = file.getName();
var fileURL  = file.getUrl();
var lastUpdated =  Utilities.formatDate(file.getLastUpdated(), timezone, "dd-MM-yyyy HH:mm");
var dateCreated =  Utilities.formatDate(file.getDateCreated(), timezone, "dd-MM-yyyy HH:mm")

row += "<li>" + lastUpdated + " <a href='" + fileURL + "'>" + fileName + "</a></li>";

sheet.appendRow([dateCreated, lastUpdated, fileName, fileURL]);

count++;
}

if (row !== "") {
row = "<p>" + count + " file(s) changed:</p><ol>" + row + "</ol>";
row +=  emailBody+"<br>" + "<br><small> "+emailFooter+" </a>.</small>";     
MailApp.sendEmail(email, emailSubject, "", {name: emailFromName, htmlBody: row});
}  
}
1

1 Answers

0
votes

I'm not sure if Drive API has email notification. However, this document describes how to use push notifications that inform your application when a resource changes. You can use this feature to improve the performance of your application. It allows you to eliminate the extra network and compute costs involved with polling resources to determine if they have changed. Whenever a watched resource changes, the Drive API notifies your application.

To use push notifications, you need to do three things:

  • Register the domain of your receiving URL.

For example, if you plan to use https://exampledomain.com/notifications as your receiving URL, you need to register https://exampledomain.com. * Set up your receiving URL, or "Webhook" callback receiver.

This is an HTTPS server that handles the API notification messages that are triggered when a resource changes.

  • Set up a notification channel for each resource endpoint you want to watch.

A channel specifies routing information for notification messages. As part of the channel setup, you identify the specific URL where you want to receive notifications. Whenever a channel's resource changes, the Drive API sends a notification message as a POST request to that URL.

For Google Drive apps that need to keep track of changes to files, follow this link: https://developers.google.com/drive/v3/web/manage-changes

I did some research bout this issue and found this Feature request. It's currently don't support notifications for folder-level changes.