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

发布于 2024-09-10 09:46:30 字数 852 浏览 0 评论 0原文

我在 SharePoint 2007 中有一个摘要单行文本列,它是多行文本列的截断。经历复杂的过程< /a> 到达那里,它会变成文本,然后需要转换回 HTML,这样

<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>

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

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 process to 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).

<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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦魇绽荼蘼 2024-09-17 09:46:30

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

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

将以下内容添加到同一页面上的内容编辑器 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>

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

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

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

<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>
娇俏 2024-09-17 09:46:30

我已经从下面的链接修改了 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>

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

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文