2017-10-15 88 views
0

我需要在AppScript中创建一个函数,用当前时间向我的电子邮件发送电子邮件。后来,我必须创建一个触发器,强制函数每小时运行一次。我知道对你们大多数人来说这很容易,但是我从编码开始,而且我坚持这个练习。Google App脚本 - 使用当前时间发送电子邮件

这是从来就写:

function sendemail(){ 

function getDate() { 
    var d = new Date(); 
    return (d.getMonth()+1) + "/" + d.getDate() + "/" + d.getFullYear(); 
} 

function getTime() { 
    var d = new Date(), 
     offset = -d.getTimezoneOffset()/60, 
     h = d.getUTCHours() + offset, 
     m = d.getMinutes(), 
     s = d.getSeconds(); 
    return h + ":" + m + ":" + s; 
} 

function getDateAndTime() { 
    return getDate() + " " + getTime(); 

    Gmail.sendEmail({ 
    to: "[email protected]", 
    subject: "Hora actual", 
    htmlBody: "La hora actual es +dateofDay <br/> Regards, Your robot", 
    }); 
} 

} 

但doesn't工作。我感到有些沮丧,并且始终出现相同的错误:“找不到脚本函数:doGet”。

任何人都可以看看它并帮助我吗?

在此先感谢,非常感谢。

最好的问候, 路易斯

+0

你是怎么调用'sendemail()' - 它是什么样的谷歌应用程序脚本[[standalone](https://developers.google.com/apps-script/guides/standalone),[bound](https ://developers.google.com/apps-script/guides/bound),[web app](https://developers.google.com/apps-script/guides/web)]?如果它是一个Web App脚本,请参阅[需求](https://developers.google.com/apps-script/guides/web#requirements_for_web_apps) –

回答

0

Web应用程序需要一个doGet(e)doPost(e)功能通过URL(doGet())或交的数据(doPost())接受从网页输入。这些函数中的参数e是传递url/post数据的'event object'。因此,您需要致电的电子邮件发送&日期生成函数。

这里的逻辑的高级视图:

function doGet(e){ 
    var d = new Date(); 
    var date = getGate(d); // your getDate() function 
    var time = getTime(d); // your getTime() function 
    var datetime = date + " " + time; 
    sendEmail(datetime); // a new function to send an email 
} 

这就是说,你可以更有效地与Utilities.formatDate(new Date())documentation here)多让你的时间戳和正确的得到您的日期&时间在正确的时区&区域设置格式。

随着发送电子邮件,您可以使用基本Mail service功能MailApp.sendEmail(recipient, subject, body),它发送电子邮件到recipient,与你&在body指定的消息传递subject。这似乎是你在这方面需要的全部。如果您需要与邮箱进行交互,那么您应该使用Gmail service,但如果您只需从运行我们应用的帐户发送电子邮件,请坚持邮件服务。

相关问题