Google apps script Google脚本:从文档生成电子邮件会丢失格式
我正在尝试从谷歌文档生成电子邮件。想法是在文档中保留一个固定的响应;随着文档的更新,固定响应也随之更新(与在脚本中硬编码相反)。然后,我的公司使用脚本从工作表中提取姓名,并向每个人发送电子邮件 然而,从上周开始,格式并没有复制。例如,如果Google文档中有粗体文本,则文本会复制到草稿中,但不会粗体。当我将HTML直接从文档复制到Gmail时,我唯一能想到的就是Gmail不尊重css。超链接可以工作,所以我确信它尊重html 如果问题是它不尊重css,我的替代方案是什么?此外,我确信这在上周起到了作用,因为我的发件箱中有我用这个脚本制作的包含粗体文本的电子邮件 示例文档(工具>脚本编辑器>运行以将其草稿放入草稿文件夹。您必须登录到Gmail并授予其权限): 脚本代码:Google apps script Google脚本:从文档生成电子邮件会丢失格式,google-apps-script,gmail,Google Apps Script,Gmail,我正在尝试从谷歌文档生成电子邮件。想法是在文档中保留一个固定的响应;随着文档的更新,固定响应也随之更新(与在脚本中硬编码相反)。然后,我的公司使用脚本从工作表中提取姓名,并向每个人发送电子邮件 然而,从上周开始,格式并没有复制。例如,如果Google文档中有粗体文本,则文本会复制到草稿中,但不会粗体。当我将HTML直接从文档复制到Gmail时,我唯一能想到的就是Gmail不尊重css。超链接可以工作,所以我确信它尊重html 如果问题是它不尊重css,我的替代方案是什么?此外,我确信这在上周起到
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,
});
}
根据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+"\"";
}
var docId=“1VnidBWgxxn1-287yo6Qf8CjKOlQhX-9qF1mIaWugTK4”;
var classArray=[];
函数makeNewEmail(){
//从文档中获取html
var subject=“测试电子邮件”;
var forDriveScope=DriveApp.getStorageUsed();//需要获取请求的驱动器作用域
变量url=”https://docs.google.com/feeds/download/documents/export/Export?id=“+docId+”&exportFormat=html”;
变量参数={
方法:“获取”,
标题:{“授权”:“承载者”+ScriptApp.getOAuthToken()},
muteHttpExceptions:true,
};
var html=UrlFetchApp.fetch(url,param.getContentText();
//docs在头部使用css,但gmail只内联使用。需要内联移动css。
//不处理头类(例如h1、h2)。
var headEnd=html.indexOf(“”);
//获取和之间的所有内容,删除引号
var head=html.substring(html.indexOf(“”+6,headEnd).replace(/“/g,”);
//在.c#上拆分任意正整数#s
var regex=/\.c\d{1,}/;
var类别=头分割(正则表达式);
//获取类信息并按类num放入数组索引。例如c4{size:small}将在classArray[4]中放入“size:small”
var总长度=0;
对于(变量i=1;i
这显然有点烦人(理想情况下,Gmail要么(1)允许内部样式表,要么(2)提供从文档导入电子邮件的功能),但它可以工作。电子邮件中的CSS需要“内联”"你不能有一个样式标签,然后用id或类名引用CSS。但是我不知道为什么它能工作,现在不行。所以它一开始就不应该工作?文档没有提供关于html应该如何构造的任何细节。所以,我没有关于CSS应该如何工作的正式答案。也许是这样我还有一个明确的答案。但是你认为一个解决方案可能是使用内联css吗?我想我会尝试内联css来确定它是否会产生影响。