2017-07-26 354 views
2

我一直在试图设置电子邮件通知,以便在我的Google网站中创建新通知。我使用了我在网上找到的基本代码,但它不适合我。它是:从Google协作平台发送电子邮件通知

function myFunction() { 

var url_of_announcements_page = "https://sites.google.com/announcements-page-link"; 
var who_to_email = "[email protected]" 

function emailAnnouncements(){ 
    var page = SitesApp.getPageByUrl(url_of_announcements_page); 
    if(page.getPageType() == SitesApp.PageType.ANNOUNCEMENTS_PAGE){ 

    var announcements = page.getAnnouncements({ start: 0, 
               max: 10, 
               includeDrafts: false, 
               includeDeleted: false}); 
    announcements.reverse();          
    for(var i in announcements) { 
     var ann = announcements[i]; 
     var updated = ann.getLastUpdated().getTime(); 
     if (updated > ScriptProperties.getProperty('last-update')){ 
     var options = {}; 

     options.htmlBody = Utilities.formatString("<h1><a href='%s'>%s</a></h1>%s", ann.getUrl(), ann.getTitle(), ann.getHtmlContent()); 

     MailApp.sendEmail(who_to_email, "Announcement "+ann.getTitle(), ann.getTextContent()+"\n\n"+ann.getUrl(), options); 

     ScriptProperties.setProperty('last-update',updated); 
     } 
    } 
    } 
} 

function setup(){ 
    ScriptProperties.setProperty('last-update',new Date().getTime()); 
} 
} 

该代码似乎运行没有出现任何错误消息。但是,我没有收到我在代码中编写的帐户上的电子邮件。我已给予完全许可,以便脚本可以从我的帐户发送电子邮件。它似乎并不能完成所需的任务。

我用来编写公告的谷歌网站仍然是私人的,只有我可以看到它,这是否在这段代码不起作用?

如果你看到任何错误或有任何想法是什么问题,我会很高兴知道。

+0

你能告诉我们你得到了什么样的错误,或者你对这段代码不起作用吗? –

+0

该代码似乎运行出现任何错误消息。但是,我没有收到我在代码中编写的帐户上的电子邮件。我已给予完全许可,以便脚本可以从我的帐户发送电子邮件。它似乎并不能完成所需的任务。 – Taum

+0

虚拟问题:您是否在网上找到了代码中的url_of_announcements_page和who_to_email变量? –

回答

1

你已经写了myFunction的两个功能。你需要单独编写它。另外ScriptPropertiesAPI已弃用,使用PropertiesService。请参阅下面的代码。希望这可以帮助!

var url_of_announcements_page = "https://sites.google.com/announcements-page-link"; 
var who_to_email = Session.getActiveUser().getEmail(); 

function emailAnnouncements(){ 
    var page = SitesApp.getPageByUrl(url_of_announcements_page); 
    if(page.getPageType() == SitesApp.PageType.ANNOUNCEMENTS_PAGE){ 

    var announcements = page.getAnnouncements({ start: 0, 
               max: 10, 
               includeDrafts: false, 
               includeDeleted: false}); 
    announcements.reverse();          
    for(var i in announcements) { 
     var ann = announcements[i]; 
     var updated = ann.getLastUpdated().getTime(); 
     if (updated > PropertiesService.getScriptProperties().getProperty("last-update")){ 
     var options = {}; 

     options.htmlBody = Utilities.formatString("<h1><a href='%s'>%s</a></h1>%s", ann.getUrl(), ann.getTitle(), ann.getHtmlContent()); 

     MailApp.sendEmail(who_to_email, "Announcement "+ann.getTitle(), ann.getTextContent()+"\n\n"+ann.getUrl(), options); 

     PropertiesService.getScriptProperties().setProperty('last-update',updated); 
     } 
    } 
    } 
} 

function setup(){ 
PropertiesService.getScriptProperties().setProperty('last-update',new Date().getTime()); 
} 
相关问题