Google apps script 如何在Gmail中使用Google电子表格内容保持表格HTML格式

Google apps script 如何在Gmail中使用Google电子表格内容保持表格HTML格式,google-apps-script,google-sheets,Google Apps Script,Google Sheets,我正在尝试开发一个脚本,它将自动发送电子邮件,其中包括来自谷歌电子表格的表格,我希望在电子邮件中保留原始表格格式/颜色。 为此,我使用了UrlFetchApp和mailApp,如下所示: var url = 'my google doc publish url'; var requestData = googleOAuth_('docs','https://docs.google.com/feeds/'); var response = UrlFetchApp.fetch(url1, reque

我正在尝试开发一个脚本,它将自动发送电子邮件,其中包括来自谷歌电子表格的表格,我希望在电子邮件中保留原始表格格式/颜色。 为此,我使用了UrlFetchApp和mailApp,如下所示:

var url = 'my google doc publish url';
var requestData = googleOAuth_('docs','https://docs.google.com/feeds/');
var response = UrlFetchApp.fetch(url1, requestData);
var me = Session.getEffectiveUser().getEmail();
var html = response.getContentText();
MailApp.sendEmail(me, 'Email subject', 'no html :( ', {'htmlBody':html}); 
<div dir=""ltr""><table cellspacing=""0"" cellpadding=""0"" dir=""ltr"" style=""table-layout:fixed;font-size:13px;font-family:arial,sans,sans-serif""><colgroup><col width=""121""><col width=""353""><col width=""87""><col width=""113""><col width=""74""></colgroup><tbody><tr style=""height:51px"">
<td style=""vertical-align:bottom;direction:ltr;background-color:rgb(182,215,168);font-family:calibri,arial,sans,sans-serif;font-weight:bold;border:1px solid rgb(0,0,0);padding:0px 3px"">Key</td>
<td style=""border-bottom-width:1px;border-top-style:solid;direction:ltr;background-color:rgb(182,215,168);border-right-style:solid;font-weight:bold;padding:0px 3px;border-bottom-color:rgb(0,0,0);border-top-width:1px;vertical-align:bottom;border-bottom-style:solid;border-top-color:rgb(0,0,0);border-right-color:rgb(0,0,0);border-right-width:1px;font-family:calibri,arial,sans,sans-serif"">Summary</td>
这实际上是可行的,我可以收到包含我需要的所有表格数据的电子邮件。但是,电子邮件中会丢失所有表格格式和颜色信息。进一步的调查表明,这可能是因为Gmail在进行HTML呈现时不支持
标记。不幸的是,谷歌发布的电子表格正在使用这一点。以下是一个例子:

<div id="content"><style>.tblGenFixed td {padding:0 3px;overflow:hidden;white-space:normal;letter-spacing:0;word-spacing:0;background-color:#fff;z-index:1;border-top:0px none;border-left:0px none;border-bottom:1px solid #CCC;border-right:1px solid #CCC;} .dn {display:none} .tblGenFixed td.s0 {background-color:#b6d7a8;font-family:calibri,arial,sans,sans-serif;font-size:100.0%;font-weight:bold;font-style:normal;color:#000000;text-decoration:none;text-align:left;vertical-align:bottom;direction:auto-ltr;white-space:normal;overflow:hidden;border-top:1px solid #000000;border-right:1px solid #000000;border-bottom:1px solid #000000;border-left:1px solid #000000;} .tblGenFixed td.s2 {background-color:#b6d7a8;font-family:calibri,arial,sans,sans-serif;font-size:100.0%;font-weight:bold;font-style:normal;color:#000000;text-decoration:none;text-align:center;vertical-align:bottom;direction:auto-ltr;white-space:normal;overflow:hidden;border-top:1px solid #000000;border-right:1px solid #000000;border-bottom:1px solid #000000;} .tblGenFixed td.s1 {background-color:#b6d7a8;font-family:calibri,arial,sans,sans-serif;font-size:100.0%;font-weight:bold;font-style:normal;color:#000000;text-decoration:none;text-align:left;vertical-align:bottom;direction:auto-ltr;white-space:normal;overflow:hidden;border-top:1px solid #000000;border-right:1px solid #000000;border-bottom:1px solid #000000;} </style><table dir='ltr' border=0 cellpadding=0 cellspacing=0 class='tblGenFixed' id='tblMain'><tr class='rShim'><td class='rShim' style='width:0;'><td class='rShim' style='width:121px;'><td class='rShim' style='width:353px;'><td class='rShim hdn' style='display:none;width:62px;'><td class='rShim' style='width:87px;'><tr dir='ltr'><td class=hd><p style='height:50px;'>.</td><td  dir='ltr' class='s0'>Key<td  dir='ltr' class='s1'>Summary<td  dir='ltr' class='s2 dn'>Labels<td  dir='ltr' class='s2'>Status</tr></table></div>
通过调试,我可以观察到这封电子邮件的HTML内容(直接从google电子表格复制粘贴到gmail composer),如下所示:

var url = 'my google doc publish url';
var requestData = googleOAuth_('docs','https://docs.google.com/feeds/');
var response = UrlFetchApp.fetch(url1, requestData);
var me = Session.getEffectiveUser().getEmail();
var html = response.getContentText();
MailApp.sendEmail(me, 'Email subject', 'no html :( ', {'htmlBody':html}); 
<div dir=""ltr""><table cellspacing=""0"" cellpadding=""0"" dir=""ltr"" style=""table-layout:fixed;font-size:13px;font-family:arial,sans,sans-serif""><colgroup><col width=""121""><col width=""353""><col width=""87""><col width=""113""><col width=""74""></colgroup><tbody><tr style=""height:51px"">
<td style=""vertical-align:bottom;direction:ltr;background-color:rgb(182,215,168);font-family:calibri,arial,sans,sans-serif;font-weight:bold;border:1px solid rgb(0,0,0);padding:0px 3px"">Key</td>
<td style=""border-bottom-width:1px;border-top-style:solid;direction:ltr;background-color:rgb(182,215,168);border-right-style:solid;font-weight:bold;padding:0px 3px;border-bottom-color:rgb(0,0,0);border-top-width:1px;vertical-align:bottom;border-bottom-style:solid;border-top-color:rgb(0,0,0);border-right-color:rgb(0,0,0);border-right-width:1px;font-family:calibri,arial,sans,sans-serif"">Summary</td>

钥匙
总结

所以我相信Gmail在粘贴的时候做了一些自动转换。因此,我的问题是如何使用谷歌脚本进行类似的转换?

没有自动的方式从gas进行转换。您需要获取每个单元格格式,并将其转换为一个html表,这不是一个简单的表(合并的单元格和其他非平凡的转换)