MS-Word插件在使用insertHTML插入链接时无法保留格式。

huangapple go评论63阅读模式
英文:

MS-Word add-in won't preserve formatting using insertHTML to insert link

问题

我正在使用JavaScript在Visual Studio中开发的Web加载项中进行Word文档的全文搜索。我的任务是在找到与某个模式匹配的特定字符串时在文档中创建自定义链接。问题在于,当我插入链接时,它会更改字体大小和类型。我正在使用的大部分测试文档都是Times New Roman,12pt。当插入链接时,其中大多数(但不是全部)的字体会更改为CG Times,10pt。

我尝试捕获字体名称和大小,并将其分配给链接,使用内联的 "style" 属性。
style="font-family: 'Times New Roman' font-size: 12px // or 12 or 12pt
我还尝试捕获样式并将其分配给链接,使用 "class" 属性。没有成功。
唯一部分成功的方法是在锚点标记内部插入链接标记,这显然是不可取的。可以保留字体,但不能保留大小。

我的循环如下:

for (let i = 0; i < searchResults.items.length; i++) {
    var sFontName = searchResults.items[i].font.name;
    var sFontSize = searchResults.items[i].font.size;
    var sStyleString = 'style=&quot;font-family: \&#39;&#39;' + sFontName + '\&#39; font-size: ' + sFontSize + '&quot;';

    var sItem = (searchResults.items[i].text);
    var sChaptSec = sItem.slice(4);
    var sChaptSecArray = sChaptSec.split(".");
    var sChapter = sChaptSecArray[0];
    var sSection = sChaptSecArray[1];
    var sLink = "http://www.example.com/BookRef/" + sChapter + "/" + sSection + "/";
    var sAnchorTag = '<a href="' + sLink + '" ' + sStyleString +
        ' target="_blank" rel="noopener noreferrer">' + sItem + '</a>';
    searchResults.items[i].insertHtml(sAnchorTag, "replace");
}

有什么建议吗?

英文:

I'm doing a full-text search of a word document using JavaScript in a web add-in I'm developing in Visual Studio. My task is to create custom links in the document when I find certain strings matching a pattern. The problem here is that when I insert the link, it changes the font size and type. Most of the test document I'm using is in Times New Roman, 12pt. When the link is inserted, the font in most of them (but not all) change to CG Times, 10pt.

I've tried capturing the font name and size and assigning it to the link using the inline "style" attribute.
style=&quot;font-family: &#39;Times New Roman&#39; font-size: 12px // or 12 or 12pt
I've also tried capturing the style and assigning it to the link using the "class" attribute. No luck.
The only thing I've done that has partly worked is to insert a link tag inside the anchor tag, which is obviously undesirable. Preserves the font, but not the size.
My for loop:

for (let i = 0; i &lt; searchResults.items.length; i++) {
                        var sFontName = searchResults.items[i].font.name;
                        var sFontSize = searchResults.items[i].font.size;
                        //var sStyle = searchResults.items[i].style;
                        //var sStyleString = &#39;class=&quot;&#39; + sStyle + &#39;\&quot; &#39;;
                        //var sFontTag = &#39;&lt;font face=&quot;\&#39;&#39; + sFontName + &#39;\&#39;&quot; size=&quot;&#39; + sFontSize + &#39;&quot; &gt;&#39;; 
                        var sStyleString = &#39;style=&quot;font-family: \&#39;&#39; + sFontName + &#39;\&#39; font-size: &#39; + sFontSize + &#39;&quot;&#39;;

                        var sItem = (searchResults.items[i].text); //(searchResults.items[i].text).replace(&#39; &#39;, &#39;_&#39;);
                        var sChaptSec = sItem.slice(4);
                        var sChaptSecArray = sChaptSec.split(&quot;.&quot;);
                        var sChapter = sChaptSecArray[0];
                        var sSection = sChaptSecArray[1];
                        var sLink = &quot;http://www.example.com/BookRef/&quot; + sChapter + &quot;/&quot; + sSection + &quot;/&quot;;
                        var sAnchorTag = &#39;&lt;a href=&quot;&#39; + sLink + &#39;&quot; &#39; + sStyleString +
                            &#39; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;&#39; + sItem + &#39;&lt;/a&gt; &#39;; //+ sFontTag     &lt;/font&gt;
                        //searchResults.items[i].hyperlink = sAnchorTag;
                        searchResults.items[i].insertHtml(sAnchorTag, &quot;replace&quot;); //(sAnchorTag, &quot;replace&quot;);
                    }

Any suggestions?

答案1

得分: 1

已解决。一旦发现,它比预期简单得多。我只需将这些值重新分配给我从中检索到的相同元素。

for (let i = 0; i < searchResults.items.length; i++) {
    var sItem = (searchResults.items[i].text);
    //获取字体名称和大小
    var sFontName = searchResults.items[i].font.name;
    var sFontSize = searchResults.items[i].font.size;
    var sChaptSec = sItem.slice(4);
    var sChaptSecArray = sChaptSec.split(".");
    var sChapter = sChaptSecArray[0];
    var sSection = sChaptSecArray[1];
    var sLink = "http://www.example.com/BookRef/" + sChapter + "/" + sSection + "/";
    var sAnchorTag = '<a href="' + sLink +
        '" target="_blank" rel="noopener noreferrer">' + sItem + "</a> ";
    searchResults.items[i].insertHtml(sAnchorTag, "replace");
    //重新分配字体名称和大小
    searchResults.items[i].font.name = sFontName;
    searchResults.items[i].font.size = sFontSize;
}
英文:

Figured it out. It was much simpler than expected once discovered. I simply had to re-assign the values to the same elements I had retrieved them from.

	for (let i = 0; i &lt; searchResults.items.length; i++) {
      var sItem = (searchResults.items[i].text);  
      //get font name and size
      var sFontName = searchResults.items[i].font.name;
      var sFontSize = searchResults.items[i].font.size;
      var sChaptSec = sItem.slice(4);
      var sChaptSecArray = sChaptSec.split(&quot;.&quot;);
      var sChapter = sChaptSecArray[0];
      var sSection = sChaptSecArray[1];
      var sLink = &quot;http://www.example.com/BookRef/&quot; + sChapter + &quot;/&quot; + sSection + &quot;/&quot;;
      var sAnchorTag = &#39;&lt;a href=&quot;&#39; + sLink + 
        &#39;&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;&#39; + sItem + &quot;&lt;/a&gt; &quot;;
      searchResults.items[i].insertHtml(sAnchorTag, &quot;replace&quot;);
      //reassign font name and size 
      searchResults.items[i].font.name = sFontName;
      searchResults.items[i].font.size = sFontSize;
    }

huangapple
  • 本文由 发表于 2023年7月6日 21:05:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76629162.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定