Google云端硬盘/电子表格 - 在子文件夹中修改文件时的电子邮件通知

时间:2016-12-01 02:57:21

标签: javascript google-drive-api email-integration

我已经设置了一个google驱动器文件夹结构,用于处理2级子文件夹。 e.g。

主文件夹

-Area 1

---排除区域

-Area 2

---排除区域

每次修改文件时我都需要收到电子邮件通知,以确保其他用户必须拥有最新的数据。我在网上发现了一个主要有效的脚本,但我需要能够在子文件夹中检查这个脚本不能做的修改。任何帮助,将不胜感激。谢谢,阿曼达

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 user@work.com";

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 个答案:

答案 0 :(得分:0)

我不确定Drive API是否有电子邮件通知。但是,此document描述了如何使用push notifications在资源发生变化时通知您的应用程序。您可以使用此功能来提高应用程序的性能。它允许您消除轮询资源所涉及的额外网络和计算成本,以确定它们是否已更改。每当观察到的资源发生变化时,Drive API都会通知您的应用程序。

要使用推送通知,您需要做三件事:

  • 注册接收网址的域名。

例如,如果您打算使用 https://exampledomain.com/notifications 作为接收网址,则需要注册 https://exampledomain.com *设置接收URL或“Webhook”回调接收器。

这是一个HTTPS服务器,用于处理资源更改时触发的API通知消息。

  • 为要监视的每个资源端点设置通知通道。

通道指定通知消息的路由信息​​。作为频道设置的一部分,您可以标识要接收通知的特定URL。每当频道的资源发生变化时,Drive API会向该网址发送一条通知消息作为POST请求。

对于需要跟踪文件更改的Google云端硬盘应用,请点击以下链接:https://developers.google.com/drive/v3/web/manage-changes

我对此问题进行了一些研究并找到了Feature request。它目前不支持文件夹级更改的通知。

相关问题