【问题标题】:Google App Script, how to get text with formatting from Docs and use it for body to send emailGoogle App Script,如何从文档中获取带有格式的文本并将其用于正文以发送电子邮件
【发布时间】:2017-03-31 07:17:49
【问题描述】:

我想使用 google docs 文件的内容从电子表格发送电子邮件作为消息(电子邮件正文)。这就是我现在所拥有的,效果很好。

*部分代码

var doc = DocumentApp.openById(row[9]);
var message = doc.getText();

MailApp.sendEmail(emailAddress, subject, "Dear " + row[6] +"\n\n"+ message + "\n\n"+ row[1] + "\n" + row[2] + "\n" + row[3] + "\n" + row[4]);

它工作得很好,但电子邮件是纯文本。

我有一点格式,例如血色/彩色/列表格式……等等。 如何在电子邮件中保留这些格式?

我尝试了 getBody() 或 getParagraph(),但它只返回“文档”或“段落、段落、段落”。

提前致谢!

*仅供参考 我试过这个。 How to send rich text emails with GmailApp? 效果不错,但我正在尝试找到一种解决方案,将富文本直接用于电子邮件。无法使用 html 格式解决。

【问题讨论】:

    标签: email google-apps-script google-docs


    【解决方案1】:
    function test_mail_() {
     var id = DocumentApp.getActiveDocument().getId();
     var url = "https://docs.google.com/feeds/download/documents/export/Export?id=" + id + "&exportFormat=html";
     var param = {
        method: "get",
        headers: {
            "Authorization": "Bearer " + ScriptApp.getOAuthToken()
        },
        muteHttpExceptions: true,
     };
     var html = UrlFetchApp.fetch(url, param).getContentText();
     var raw = Utilities.base64EncodeWebSafe("Subject: Test\r\n" +
        "From: " + Session.getActiveUser().getEmail() + "\r\n" +
        "To: test@gmail.com\r\n" +
        "Content-Type: text/html; charset=UTF-8\r\n\r\n" +
        html + "\r\n\r\n");
     var message = Gmail.newMessage();
     message.raw = raw;
     var sentMsg = Gmail.Users.Messages.send(message, 'test@gmail.com');
    } 
    

    这对我有用。希望这可能会有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-15
      • 2020-04-18
      • 2014-10-03
      • 1970-01-01
      • 1970-01-01
      • 2020-10-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多