我一直在尝试设置电子邮件通知,以便在我的谷歌网站中创建新公告时。我使用了我在网上找到的基本代码,但它对我不起作用。在这里:
function myFunction() {
var url_of_announcements_page = "https://sites.google.com/announcements-page-link";
var who_to_email = "name@company.com"
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()+"nn"+ann.getUrl(), options);
ScriptProperties.setProperty('last-update',updated);
}
}
}
}
function setup(){
ScriptProperties.setProperty('last-update',new Date().getTime());
}
}
代码似乎运行时没有出现任何错误消息。但是,我没有收到我在代码中编写的帐户的电子邮件。我已授予完全权限,以便脚本可以从我的帐户发送电子邮件。它似乎没有完成所需的任务。
我用来编写公告的谷歌网站仍然是私有的,只有我可以看到它,这是否在此代码不起作用中起作用?
如果您看到任何错误或知道问题是什么,我很乐意知道。
您已经在myFunction
下编写了两个函数。您需要单独编写。此外,ScriptProperties
API 已弃用,请使用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()+"nn"+ann.getUrl(), options);
PropertiesService.getScriptProperties().setProperty('last-update',updated);
}
}
}
}
function setup(){
PropertiesService.getScriptProperties().setProperty('last-update',new Date().getTime());
}