使用Google Apps脚本在Google电子邮件正文中将Google文档作为HTML发送,同时保留格式

时间:2017-03-06 12:40:17

标签: html email google-apps-script

在Google文档上使用以下脚本,我尝试将文档作为HTML发送到电子邮件正文中。它正确地转换文档(当我通过URL检查导出的文档时)并发送具有相同内容的电子邮件,但它在某些时候丢失了以下格式:字体格式(例如,大小,颜色)和表格格式(例如,边框,背景颜色)

function sendGoogleDocAsHTML(){
  var id = DocumentApp.getActiveDocument().getId() ;
  var url = "https://docs.google.com/document/d/"+id+"/export?format=html"
  var param = {
method      : "get",
headers     : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
muteHttpExceptions:true,
  };
  var html = UrlFetchApp.fetch(url, param);
  var email = <EMAIL>;
  var subject = <SUBJECT>;
  GmailApp.sendEmail(email, subject,"", {htmlBody:html});
}

如何保留电子邮件中的格式?

1 个答案:

答案 0 :(得分:2)

这对我来说很好。希望这可能会有所帮助

  function sendGoogleDocAsHTML(){
  var id = DocumentApp.getActiveDocument().getId() ;
  var url = "https://docs.google.com/document/d/"+id+"/export?format=html"
  var param = {
      method      : "get",
      headers     : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
      muteHttpExceptions:true,
  };
  var html = UrlFetchApp.fetch(url, param);
  var raw = Utilities.base64EncodeWebSafe("Subject: Test\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, 'me');
}