2013-05-09 131 views
0

我有一個簡單的文檔/模板,我想在向用戶發送電子郵件時使用。它適用於文本版本,但我無法弄清楚,如何以HTML格式發送文檔。該文件只包含帶有粗體格式的文字。Google Apps腳本MailApp使用htmlBody文檔

function sendEmail(emailAddress, attachment){ 
    var EMAIL_TEMPLATE_ID = "SOME_GOOGLE_DOC_ID"; 
    var emailTemplate = DocumentApp.openById(EMAIL_TEMPLATE_ID); 
    MailApp.sendEmail(emailAddress, "Subject", emailTemplate.getText(), { 
     htmlBody: emailTemplate, <-- THIS does not return correct data }); 
} 

回答

0

htmlBody參數期望的字符串而emailTemplate變量的類型爲文檔。

你應該使用類似

MailApp.sendEmail(emailAddress, "Subject", emailTemplate.getText(), { 
     htmlBody: emailTemplate.getBody().getText() }); 
+0

在htmlBody的格式化是完全喪失(它被接收爲簡單的字符串)。 這就是模板的外觀(簡單文本用斜體,粗體和進入):https://docs.google.com/document/d/1IkJhOVCTs66F_4LJwMtmx0EM_QkNkunYYiHqT1MCwTw/edit?usp=sharing – user2365226 2013-05-09 08:43:23

+0

您應該用HTML編寫文檔。 – Srik 2013-05-10 02:11:02

0

這是相當直接的,如果我們利用羅馬Vialard的DriveApp庫。您需要按照說明here將庫添加到您的項目中,然後您可以使用下面的代碼。

/** 
* get a String containing the contents of the given document as HTML. 
* Uses DriveApp library, key Mi-n8njGmTTPutNPEIaArGOVJ5jnXUK_T. 
* 
* @param {String} docID ID of a Google Document 
* 
* @returns {String} Content of document, rendered in HTML. 
* 
* @see https://sites.google.com/site/scriptsexamples/new-connectors-to-google-services/driveservice 
*/ 
function getDocAsHTML(docID) { 
    var doc = DriveApp.getFileById(docID); 
    var html = doc.file.content.src; 
    var response = UrlFetchApp.fetch(html); 
    var template = response.getContentText(); 

    return template; 
} 

function sendEmail(emailAddress, attachment){ 
    var EMAIL_TEMPLATE_ID = 'SOME_GOOGLE_DOC_ID'; 
    var emailTemplate = getDocAsHTML(EMAIL_TEMPLATE_ID); 
    MailApp.sendEmail(emailAddress, "Subject", emailTemplate.getText(), { 
     htmlBody: emailTemplate }); 
} 
2

我發現答案是這樣的一個

function getDocAsHtml(docId){ 
    var url = 'https://docs.google.com/feeds/download/documents/Export?exportFormat=html&format=html&id='; 
    return UrlFetchApp.fetch(url+docId).getContentText(); 
} 

工作得很好

相關問題