2017-10-15 45 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,但如果您只需從運行我們應用的帳戶發送電子郵件,請堅持郵件服務。

相關問題