Google脚本:从文档生成电子邮件会丢失格式

问题描述 投票:0回答:1

我正在尝试从Google文档生成电子邮件。我们的想法是在Doc中保留一个预制的响应;随着Doc的更新,预制响应也是如此(而不是在脚本中对其进行硬编码)。然后我的公司使用脚本从工作表中提取名称,并向每个人发送一封电子邮件。

但是,截至上周,格式化不会复制。例如,如果Google文档中有粗体文本,则会将文本复制到草稿中,但不会加粗。当我将HTML直接从Doc复制到Gmail时,我唯一能想到的是Gmail不尊重CSS。超链接工作,所以我敢肯定它尊重HTML。

如果问题是它不尊重css,我的替代方案是什么?此外,我确信这在上周工作,因为我在我的发件箱中使用包含粗体文本的脚本制作了电子邮件。

示例文档(工具>脚本编辑器>运行以将其草稿放入草稿文件夹。您必须登录Gmail并授予其权限):

https://docs.google.com/document/d/15Y4lGHq-gsftz6JEzpvql57ZOBTe9CWgk0x29OW0onc/edit

脚本代码:

var docId="15Y4lGHq-gsftz6JEzpvql57ZOBTe9CWgk0x29OW0onc";

function makeNewEmail(){
  var subject="Test Email";
  var forDriveScope = DriveApp.getStorageUsed(); //needed to get Drive Scope requested
  var url = "https://docs.google.com/feeds/download/documents/export/Export?id="+docId+"&exportFormat=html";
  var param = {
    method      : "get",
    headers     : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
    muteHttpExceptions:true,
  };
  var html = UrlFetchApp.fetch(url,param).getContentText();

  GmailApp.createDraft(Session.getActiveUser().getEmail(), subject, "HTML is not enabled in your email client. Sad face!", {
    htmlBody: html,
  });  
}
google-apps-script gmail
1个回答
0
投票

Per Sandy Good的建议是,我能够通过让脚本将内部样式表移动到内联来格式化电子邮件。

请注意,它目前只处理.c标记(没有h标记)并假设head不以.c标记开头。

var docId="1VnidBWgxxn1-287yo6Qf8CjKOlQhX-9qF1mIaWugTK4";
var classArray=[];

function makeNewEmail(){
  //get html from Doc
  var subject="Test Email";
  var forDriveScope = DriveApp.getStorageUsed(); //needed to get Drive Scope requested
  var url = "https://docs.google.com/feeds/download/documents/export/Export?id="+docId+"&exportFormat=html";
  var param = {
    method      : "get",
    headers     : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
    muteHttpExceptions:true,
  };
  var html = UrlFetchApp.fetch(url,param).getContentText();

  //docs uses css in the head, but gmail only takes it inline. need to move css inline.
  //DOES NOT HANDLE HEADER CLASSES (eg h1, h2).
  var headEnd = html.indexOf("</head>");
  //get everything between <head> and </head>, remove quotes
  var head = html.substring(html.indexOf("<head>")+6,headEnd).replace(/"/g,"");
  //split on .c# with any positive integer amount of #s
  var regex = /\.c\d{1,}/;
  var classes = head.split(regex);
  //get class info and put in an array index by class num. EG c4{size:small} will put "size:small" in classArray[4]
  var totalLength = 0;
  for(var i = 1; i < classes.length; i++){
    //assume the first string (classes[0]) isn't a class definition
    totalLength = totalLength + classes[i-1].length;
    var cNum = head.substring(totalLength+2,head.indexOf("{",totalLength)); //totallength+2 chops off .c, so get what's between .c and {
    totalLength = totalLength + 2 + cNum.length //add .c and the number of digits in the num
    classArray[cNum] = classes[i].substring(1,classes[i].indexOf("}")); //put what's between .c#{ and } in classArray[#]
  }

  //now we have the class definitions, let's put it in the html  
  html = html.substring(headEnd+7,html.indexOf("</html>")); //get everything between <html> and </html>
  var classMatch = /class=\"(c\d{1,} ){0,}(c\d{1,})\"/g
  //matches class="c# c#..." where c#[space] occurs any number of times, even zero times, and c#[no space] occurs after it, exactly once
  html = html.replace(classMatch,replacer); //replace class="c# c#..." with the definitions in classArray[#]

  //make the e-mail!
  GmailApp.createDraft(Session.getActiveUser().getEmail(), subject, "HTML is not enabled in your email client. Sad face!", {
    htmlBody: html,
  });
}

function replacer(match){
  var csOnly = match.substring(7,match.length-1); //class=" has 7 chars, remove the last "
  var cs = csOnly.split(" "); //get each c#
  var ret = "style=\""
  for(var cCount = 0; cCount < cs.length; cCount++){
    ret = ret + classArray[cs[cCount].substring(1)];
  }
  return ret+"\"";
}

这显然有点烦人(理想情况下,Gmail会(1)允许内部样式表或(2)提供从Docs导入电子邮件的功能),但它有效。

© www.soinside.com 2019 - 2024. All rights reserved.