2017-02-20 103 views
1

我在從另一個電子郵件地址(它是別名)的Google工作表中發送html正文消息時遇到問題。從其他電子郵件地址通過Google表格發送包含HTML正文的電子郵件

我可以使用mailApp發送它,但是當我切換到GmailApp時,我似乎無法使其工作。

我使用的腳本如下:

function sendNotification(event) { 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var sheet = ss.getActiveSheet(); 
    var row = sheet.getActiveRange().getRow(); 
    var cellvalue = ss.getActiveCell().getValue().toString(); 
    var emailAdd = "[email protected]"; 
    if(event.range.getA1Notation().indexOf("G") > -1 && sheet.getRange("G" + row).getDisplayValue() > 999 && emailAdd.length > 1) 
    { 
    var rowVals = getActiveRowValues(sheet); 
    var aliases = GmailApp.getAliases(); 
Logger.log(aliases); 
    GmailApp.sendEmail(
     "[email protected]", 
     "Allocation Request - " + rowVals.quantity + " cases on " + rowVals.date, 
     {htmlBody: "There has been a new allocation request from " + rowVals.name + " in the " + rowVals.team + " team.<br \> <br \> " 
     + "<table border = \"1\" cellpadding=\"10\" cellspacing=\"0\"><tr><th>Issuing Depot</th><th>Delivery Date</th><th>Case Quantity</th></tr><tr><td>"+rowVals.depot+"</td><td>"+rowVals.date+"</td><td>"+rowVals.quantity+"</td></tr></table>" 
     + "<br \>To view the full details of the request, use the link below.<br \> <br \>" + 
     "<a href=\"https://docs.google.com/spreadsheets\">Allocation Requests</a>" 
     +"<br \> <br \><i>This is an automated email. Please do not reply to it.<\i>"}, 
     {from: aliases[0]} 
         ); 
    } 
} 

    function getActiveRowValues(sheet){ 
    var cellRow = sheet.getActiveRange().getRow(); 
    // get depot value 
    var depotCell = sheet.getRange("E" + cellRow); 
    var depot = depotCell.getDisplayValue(); 
    // get date value 
    var dateCell = sheet.getRange("F" + cellRow); 
    var date = dateCell.getDisplayValue(); 
    // get quantity value 
    var quantCell = sheet.getRange("G" + cellRow); 
    var quant = quantCell.getDisplayValue(); 
    // return an object with your values 
    var nameCell = sheet.getRange("B" + cellRow); 
    var name = nameCell.getDisplayValue(); 
    var teamCell = sheet.getRange("C" + cellRow); 
    var team = teamCell.getDisplayValue(); 
    return { 
    depot: depot, 
    date: date, 
    quantity: quant, 
    name: name, 
    team: team 
    } } 

我設法從我的別名發送電子郵件,但它只是發送和包含的消息[對象],而不是從它發送別名工作正常。

有人可以看看我做錯了嗎?我還沒有在這裏找到答案。謝謝。

+0

有4個參數來sendEmail(收件人,主題,郵件正文,OPTIONS)你必須在電子郵件正文中的第三個參數選項對象。你可能犯了這個錯誤,因爲你試圖把所有東西放在一起。您應該爲選項對象和Html創建一個變量,以便sendEmail()更具可讀性。 –

+0

謝謝,注意到我的錯誤。但是,如何將變量設置爲html正文? –

+0

看到回答下面 –

回答

1

創建一個對象,然後添加元素對象:

var bodyHTML,o,sendTO,subject;//Declare variables without assigning a value 

o = {};//Create an empty object 

bodyHTML = "There has been a new allocation request from " + rowVals.name; 

o.htmlBody = bodyHTML;//Add the HTML to the object with a property name of htmlBody 
o.from = aliases[0]; //Add the from option to the object 

sendTO = "[email protected]"; 
subject = "Allocation Request - " + rowVals.quantity + " cases on " + rowVals.date; 

GmailApp.sendEmail(sendTO,subject,"",o);//Leave the third parameter as an empty string because the htmlBody advanced parameter is set in the object. 
+0

對不起,有點愚蠢,似乎無法得到這個工作。我對腳本編程很陌生,所以顯然做錯了什麼。我在哪裏添加到我現有的腳本,使其工作? –

+0

變量聲明應該在最上面。我在'sendMail'前面留下了'GmailApp',我剛剛添加了一個'sendTO'變量。你是否收到錯誤信息?查看VIEW,EXECUTION TRANSCRIPT查看代碼是否完成,如果失敗,那麼失敗的代碼行。 –

+0

我沒有收到錯誤消息,但是在執行腳本中查找失敗。執行失敗:TypeError:無法在對象中找到函數sendMail –

相關問題