我在编写邮件时使用Office.js插入签名。我的问题是,在第一次插入时,它将字体和字体大小更改为错误的值,如果我再次插入或插入一些其他签名,而不是正确插入它。 在此特定示例中,Outlook中的所有设置都设置为Arial 11,我要插入的签名也是Arial 11.
function mergeAndInsertHtmlTemplate(_html, _json, isSignature, templateId, onDoneCallback, directMerge) {
getMailBody(function (_htmlBody) {
if (!_htmlBody.startsWith("<template>")) {
var t = document.createElement("template");
t.innerHTML = _htmlBody;
_htmlBody = t.outerHTML;
}
// template html merged with data
var _mergedHtml = mergeHtmlAndJson(_html, _json);
if (isSignature) {
var mergedSignature = mergeHtmlSignature(_htmlBody, _mergedHtml);
if (mergedSignature.succeeded) {
updateMailBody(mergedSignature.html, function (result) {
factory.PrependData("");
});
}
else {
insertAtCursorLocation(_mergedHtml); // insert at cursor location
}
}
else {
if (Object.keys(_json).length > 0 && !directMerge) {
_mergedHtml = mergeMustachAndJson(_htmlBody, _json, true);
updateMailBody(_mergedHtml);
}
else {
_mergedHtml = mergeMustachAndJson(_html, _json);
insertAtCursorLocation(_mergedHtml);
}
}
if (onDoneCallback) onDoneCallback();
});
}
function mergeHtmlSignature(mailBodyHtml, signatureHtml) {
if (!mailBodyHtml.startsWith("<template>")) {
var t = document.createElement("template");
t.innerHTML = mailBodyHtml;
mailBodyHtml = t.outerHTML;
}
var $mailBody = $(mailBodyHtml);
var endComposeIndex = $mailBody.find("*").index($mailBody.find('a[name=_MailEndCompose]'));
// $mailBody.find('[style*="_MailAutoSig"]').parent().not('div').empty(); // tryout: signature inserted twice
var toRemove = [];
$mailBody.find('*[class*=iw-signature]:not(:has(a[name=_MailAutoSig]))').each(function () {
var thisIndex = $mailBody.find("*").index($(this));
if(endComposeIndex < 0 || thisIndex < endComposeIndex){//if ($(this).prevAll().filter("a[name=_MailEndCompose]").length == 0) {
var parentTables = $(this).parents("table");
toRemove.push(parentTables);
toRemove.unshift($(this));
}
});
for (var i = 0; i < toRemove.length; i++) {
toRemove[i].remove();
}
$mailBody.find('[style*="_MailAutoSig"]').each(function () {
var parentTables = $(this).parents("table");
toRemove.push(parentTables);
$(this).remove();
});
for (var i = 0; i < toRemove.length; i++) {
toRemove[i].remove();
}
var _signatureLocations = $mailBody.find('a[name=_MailAutoSig]');
if (_signatureLocations && _signatureLocations.length > 0) {
_signatureLocations.parent().empty().html(signatureHtml);
return { html: $mailBody[0].outerHTML, succeeded: true }
}
else {
$log.warn('No signature location to insert signature. Inserting at cursor location');
return { html: signatureHtml, succeeded: false };
}
}
function insertAtCursorLocation(data) {
if (factory.BodyType === MailBody.HTML) {
// Body is of HTML type. Specify HTML in the coercionType parameter of setSelectedDataAsync.
Office.context.mailbox.item.body.setSelectedDataAsync(data,
{
coercionType: Office.CoercionType.Html,
asyncContext: { var3: 1, var4: 2 }
},
function (asyncResult) {
if (asyncResult.status === Office.AsyncResultStatus.Failed) {
$log.error(asyncResult.error.message);
}
});
}
else {
Office.context.mailbox.item.body.setSelectedDataAsync(data,
{
coercionType: Office.CoercionType.Text,
asyncContext: { var3: 1, var4: 2 }
},
function (asyncResult) {
if (asyncResult.status === Office.AsyncResultStatus.Failed) {
$log.error(asyncResult.error.message);
}
});
}
}
function updateMailBody(data, callback) {
Office.context.mailbox.item.body.setAsync(data, { coercionType: factory.BodyType }, function (result) {
if (callback) callback(result);
});
}
这是我要插入的签名:
<div>
<a name="_MailAutoSig">
<div class="iw-signature" style="-aw-headerfooter-type: header-primary;">
<p class="iw-signature" style="margin: 0pt;">
<span class="iw-signature" style="font-family: Calibri; font-size: 11pt;"> </span>
</p>
</div><p class="iw-signature" style="margin: 0pt 0pt 8pt; line-height: 108%; font-size: 11pt;">
<span class="iw-signature" style="font-family: Arial; font-size: 11pt;">Kind regards,</span>
</p><p class="iw-signature" style="margin: 0pt 0pt 8pt; line-height: 108%; font-size: 11pt;">
<span class="iw-signature" style="font-family: Arial; font-size: 11pt;">This one is also with ARIAL 11.</span>
</p><p class="iw-signature" style="margin: 0pt 0pt 8pt; line-height: 108%; font-size: 11pt;">
<span class="iw-signature" style="font-family: Arial; font-size: 11pt;">Byeeeeeeeeeeee</span>
</p>
<div class="iw-signature" style="-aw-headerfooter-type: footer-primary;">
<p class="iw-signature" style="margin: 0pt;">
<span class="iw-signature" style="font-family: Calibri; font-size: 11pt;"> </span>
</p>
</div>
</a>
我正在这样做,而邮件正文是空的,所以邮件正文有默认值,函数insertAtCursorLocation(data)被调用,其中数据是签名html本身。
您可以在下一张图片上看到字体样式和大小:
signature line when first inserted
Signature line when inserted for second time
你能否告诉我解决这个问题的最佳方法是什么?即使它是解决方法,对我来说也意味着很多。