使用 Javascript 在 SharePoint 中将文本转换为 HTML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3269039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 00:42:07  来源:igfitidea点击:

Text to HTML in SharePoint using Javascript

javascriptsharepointtext-to-htmlenhanced-rich-text

提问by Lance Roberts

I have a summary single-line text column in SharePoint 2007 that is a truncation of a multi-line text column. Going through the complicated processto get there, it turns into text which then needs to be converted back to HTML, so that the tags like <div>don't show. The following code works if the multi-line column is rich text, but not if it's enhanced rich text. Does anyone have the code handy to make this work? (Note: I am working on it but haven't really done any javascript up until now, so it's slow going).

我在 SharePoint 2007 中有一个摘要单行文本列,它是多行文本列的截断。通过复杂的过程到达那里,它变成了文本,然后需要将其转换回 HTML,以便<div>不显示诸如此类的标签。如果多行列是富文本,则以下代码有效,但如果是增强型富文本则无效。有没有人手头有代码来完成这项工作?(注意:我正在研究它,但直到现在还没有真正做过任何 javascript,所以它进展缓慢)。

<script type="text/javascript">
  var theTDs = document.getElementsByTagName("TD");
  var i=0;
  var TDContent = " ";
  while (i < theTDs.length)
  {
    try
    {
      TDContent = theTDs[i].innerText || theTDs[i].textContent;
      if (TDContent.indexOf("<div") == 0)
        {
          theTDs[i].innerHTML = TDContent;
        }
    }
  catch(err){}
  i=i+1;
  }
</script>

The result I'm getting now is nothing visible, because with enhanced rich text the div tag is longer than my 45 character truncation limit.

我现在得到的结果是不可见的,因为使用增强的富文本 div 标签比我的 45 个字符截断限制长。

回答by Ryan

How about using Christophe's techniques to output HTML using a calculated column.

如何使用 Christophe 的技术使用计算列输出 HTML

Specifically he has written javascript that will turn the encoded HTML (which you've now got) into HTML.

具体来说,他编写了 javascript 将编码的 HTML(您现在已经获得)转换为 HTML。

Add the following into a Content Editor Web Part (CEWP) on the same page.

将以下内容添加到同一页面上的内容编辑器 Web 部件 (CEWP)。

<script type="text/javascript">
/*
Text to HTML Lite - version 2.1.1
Questions and comments: [email protected]
*/

function TextToHTML(NodeSet, HTMLregexp) {
   var CellContent = "";
   var i=0;
   while (i < NodeSet.length)
   {
      try 
      {
         CellContent = NodeSet[i].innerText || NodeSet[i].textContent;
         if (HTMLregexp.test(CellContent)) 
            { NodeSet[i].innerHTML = CellContent; }
      } 
      catch(err)
      {}

      i=i+1;
   }
}

// Calendar views
var regexpA = new RegExp("\s*<([a-zA-Z]*)(.|\s)*/\1?>\s*");
TextToHTML(document.getElementsByTagName("a"),regexpA);

// List views
var regexpTD = new RegExp("^\s*<([a-zA-Z]*)(.|\s)*/\1?>\s*$");
TextToHTML(document.getElementsByTagName("TD"),regexpTD);

</script>

回答by Tamás J. Schilling

I have modified the TextToHTML code from below link, source is PathToSharePoint.com and I have added an event listener which works on SharePoint 2016 successfully in IE compatiblity mode which runs as IE10 and Chrome latest version: Text to Html conversion in Sharepoint 2010

我从下面的链接修改了 TextToHTML 代码,源是 PathToSharePoint.com,我添加了一个事件侦听器,它在 IE 兼容模式下成功地在 SharePoint 2016 上运行,该模式作为 IE10 和 Chrome 最新版本运行:Sharepoint 2010 中的文本到 Html 转换

<script type="text/javascript">
  /*
  Text to HTML Lite - version 2.1.1
  Questions and comments: [email protected]
  */
  document.addEventListener("DOMContentLoaded", function() {
    function TextToHTML(NodeSet, HTMLregexp) {
      var CellContent = "";
      var i = 0;
      while (i < NodeSet.length) {
        try {
          CellContent = NodeSet[i].innerText || NodeSet[i].textContent;
          if (HTMLregexp.test(CellContent)) {NodeSet[i].innerHTML = CellContent;}
        }
        catch (err) {}
        i = i + 1;
      }
    }

    // Calendar views
    var regexpA = new RegExp("\s*<([a-zA-Z]*)(.|\s)*/\1?>\s*");
    TextToHTML(document.getElementsByTagName("a"), regexpA);

    // List views
    var regexpTD = new RegExp("^\s*<([a-zA-Z]*)(.|\s)*/\1?>\s*$");
    TextToHTML(document.getElementsByTagName("TD"), regexpTD);

    // This function is call continuesly every 100ms until the length of the main field changes
    // after which the convert text to HTML is executed.
    var postElemLength = 0;
    function PostConvertToHtml() {
      if (postElemLength == document.getElementsByTagName("TD").length) {
        setTimeout(PostConvertToHtml, 100);
      }
      else {
        var regexpTD = new RegExp("^\s*<([a-zA-Z]*)(.|\s)*/\1?>\s*$");
        TextToHTML(document.getElementsByTagName("TD"), regexpTD);
      }
    }

    // Grouped list views
    ExpGroupRenderData = (function(old) {
      return function(htmlToRender, groupName, isLoaded) {

        var result = old(htmlToRender, groupName, isLoaded);
        var regexpTD = new RegExp("^\s*<([a-zA-Z]*)(.|\s)*/\1?>\s*$");
        TextToHTML(document.getElementsByTagName("TD"), regexpTD);
      };
    })(ExpGroupRenderData);

    // Preview pane views
    if (typeof (showpreview1) == "function") {
      showpreview1 = (function(old) {
        return function(o) {
          var result = old(o);
          var regexpTD = new RegExp("^\s*<([a-zA-Z]*)(.|\s)*/\1?>\s*$");
          TextToHTML(document.getElementsByTagName("TD"), regexpTD);
        };
      })(showpreview1);
    }
  });
</script>