如何从任何浏览器复制邮件合并字段并将其粘贴到MS-Word中?我正在尝试构建一个网页,您只需单击按钮或链接即可从页面复制邮件合并字段,并将其粘贴到MS-Word中。
我已经编写了HTML和Javascript来完成此任务,但它只适用于IE。请参阅底部的代码。 (我使用的是clipboard.js。)
我受到以下限制:
问题是当我从Chrome复制时,剪贴板中的html文本与IE不同,它正在更改样式,以便当我粘贴到Word时,它只是将其视为文本而不是字段代码。
下面是我使用特殊粘贴到notepad ++中检索到的剪贴板html文本的示例。
Internet Explorer
Version:1.0
StartHTML:000000219
EndHTML:000000626
StartFragment:000000350
EndFragment:000000568
StartSelection:000000350
EndSelection:000000568
SourceURL: .../MiniTag.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD></HEAD><BODY><TABLE><TBODY><TR><TD><!--StartFragment--><DIV id="divAccountNumber"><!--StartFragment--><P class="MsoNormal"><SPAN style='mso-field-code: "MERGEFIELD AccountNumber";'><SPAN style="mso-no-proof: yes;">«AccountNumber»</SPAN></SPAN></P><!--EndFragment--></DIV><!--EndFragment--></TD></TR></TBODY></TABLE></BODY></HTML>
Chrome剪贴板
Version:0.9
StartHTML:0000000173
EndHTML:0000000684
StartFragment:0000000209
EndFragment:0000000648
SourceURL: .../MiniTag.html
<html>
<body>
<!--StartFragment--><span style="color: rgb(0, 0, 0); font-family: "Times New Roman"; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; display: inline !important; float: none;">«AccountNumber»</span><!--EndFragment-->
</body>
</html>
HTML和Javascript代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD></HEAD>
<BODY>
<!-- 1. Define some markup -->
<table>
<tr>
<td><div id="divAccountNumber"></div></td>
<td><button id="btnAccountNumber" class="btn" data-clipboard-action="copy">Copy</button></td>
</tr>
</table>
</pre>
<!-- 2. Include library -->
<script src="./dist/clipboard.min.js"></script>
<!-- 3. Instantiate clipboard -->
<script>
var clipboard = new Clipboard('.btn',{
target: function(trigger){
document.getElementById('divAccountNumber').innerHTML = getMiniHtml();
return document.getElementById('divAccountNumber');
}
});
function getMiniHtml()
{
//« is «
//» is »
//so on UI, mail merge tag will rendered as «AccountNumber»
var tagText = "";
tagText += "<!--StartFragment-->"
tagText += "<p class='MsoNormal'><span style='mso-field-code:\"MERGEFIELD AccountNumber\"'><span style='mso-no-proof:yes'>«AccountNumber»</span></span></p>";
tagText += "<!--EndFragment-->";
return tagText;
}
clipboard.on('success', function(e) {
alert('copied');
console.log(e);
});
clipboard.on('error', function(e) {
alert('error occurred');
console.log(e);
});
</script>
</BODY>
</HTML>
&#13;