问题描述
我正在研究Word加载项,该加载项将选定的数据作为HTML进行获取,进行一些更改并将其重新设置为HTML。它可以在Windows上的MS Word上正常工作,但在Web版本上,它将所选文本的字体从Calibri
更改为Segoe UI
。同样,再次仅在网络版本中,它在末尾插入了额外的新行。知道如何解决该问题吗?
var html = '';
// https://stackoverflow.com/questions/41539946/how-to-replace-text-with-a-ms-word-web-add-in-by-preserving-the-formatting
Office.context.document.getSelectedDataAsync(Office.CoercionType.Html,function (asyncResult) {
if (asyncResult.status == Office.AsyncResultStatus.Failed) {
write('Action failed. Error: ' + asyncResult.error.message);
}
else {
html = asyncResult.value;
//Make some corrections on the text without touching the formatting.
let new_html = html.replace('is an','is a');
Office.context.document.setSelectedDataAsync(new_html,{ 'coercionType': Office.CoercionType.Html },function (asyncResult) {
if (asyncResult.status == Office.AsyncResultStatus.Failed) {
write(asyncResult.error.message);
}
else{
console.log('success');
}
});
}
});
注意:我使用HTML强制类型的原因是我不想丢失格式。我曾尝试使用OOXML强制类型进行相同的操作,这没有相同的问题,但是它可以清除撤消/重做堆栈,并且无法撤消通过加载项完成的更改。
HTML之前:
<HTML>
<HEAD></HEAD>
<BODY>
<div class="OutlineGroup">
<div class="OutlineElement Ltr">
<div class="ParaWrappingDiv">
<p class="Paragraph" xml:lang="EN-US" lang="EN-US" paraid="0" paraeid="{3cf43d0f-1abb-48c2-bf39-9f6530096a44}{206}" style="font-weight: normal; font-style: normal; vertical-align: baseline; font-family: "Segoe UI",Tahoma,Verdana,Sans-Serif; background-color: transparent; color: windowtext; text-align: left; margin: 0px 0px 10.6667px; padding-left: 0px; padding-right: 0px; text-indent: 0px; font-size: 6pt;">
<span data-contrast="auto" class="TextRun" xml:lang="EN-US" lang="EN-US" style="font-size: 11pt; font-family: WordVisi_MSFontService,Calibri,Calibri_EmbeddedFont,Calibri_MSFontService,sans-serif; font-kerning: none; line-height: 19.425px;">
<span class="NormalTextRun" style="background-color: inherit;">This </span>
</span>
<span data-contrast="auto" class="TextRun" xml:lang="EN-US" lang="EN-US" style="font-weight: bold; font-size: 11pt; font-family: WordVisi_MSFontService,sans-serif; font-kerning: none; line-height: 19.425px;">
<span class="NormalTextRun" style="background-color: inherit;">is an</span>
</span>
<span data-contrast="auto" class="TextRun" xml:lang="EN-US" lang="EN-US" style="font-size: 11pt; font-family: WordVisi_MSFontService,sans-serif; font-kerning: none; line-height: 19.425px;">
<span class="NormalTextRun" style="background-color: inherit;"> test.</span>
</span>
<span class="EOP" style="font-size: 11pt; line-height: 19.425px; font-family: WordVisiPilcrow_MSFontService,sans-serif;"> </span>
</p>
</div>
</div>
</div>
<span class="WACImageGroupContainer"></span>
<span data-contrast="auto" class="TextRun" xml:lang="EN-US" lang="EN-US" style="font-size: 11pt; font-family: WordVisi_MSFontService,sans-serif; font-kerning: none; line-height: 19.425px;"></span>
<span class="NormalTextRun" style="background-color: inherit;"></span>
<span data-contrast="auto" class="TextRun" xml:lang="EN-US" lang="EN-US" style="font-weight: bold; font-size: 11pt; font-family: WordVisi_MSFontService,sans-serif; font-kerning: none; line-height: 19.425px;"></span>
</BODY>
</HTML>
HTML之后:
<HTML>
<HEAD></HEAD>
<BODY>
<div class="OutlineGroup">
<div class="OutlineElement Ltr">
<div class="ParaWrappingDiv">
<p class="Paragraph" xml:lang="EN-US" lang="EN-US" paraid="0" paraeid="{3cf43d0f-1abb-48c2-bf39-9f6530096a44}{206}" style="font-weight: normal; font-style: normal; vertical-align: baseline; font-family: "Segoe UI",sans-serif; font-kerning: none; line-height: 19.425px;">
<span class="NormalTextRun" style="background-color: inherit;">is a</span>
</span>
<span data-contrast="auto" class="TextRun" xml:lang="EN-US" lang="EN-US" style="font-size: 11pt; font-family: WordVisi_MSFontService,sans-serif; font-kerning: none; line-height: 19.425px;"></span>
</BODY>
</HTML>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)