【问题标题】:Check Google Document text for URL检查 URL 的 Google 文档文本
【发布时间】:2017-10-19 10:30:21
【问题描述】:

我们有一个 Google 脚本,它作为插件运行并将基本格式转换为基本 HTML。

但是,当链接是一个完整的句子时,我似乎无法检测到它们。

应该找到链接的函数;

function processText(item, output) {
var text = item.getText();
var indices = item.getTextAttributeIndices();

Logger.log("processText. "+item+". "+text);

if (indices.length <= 1) {
   var partAtts = item.getAttributes(indices[0]);

// Assuming that a whole para fully italic is a quote
if(item.isBold()) {
  output.push('<b>' + text + '</b>');
}
else if(item.isItalic()) {
  output.push('<blockquote>' + text + '</blockquote>');
}
else if (text.trim().indexOf('http://') > -1) {
  output.push('<a href="' + text + '" rel="nofollow" class="a">' + text + '</a>');
}
else if (text.trim().indexOf('https://') > -1) {
  output.push('<a href="' + text + '" rel="nofollow" class="b">' + text + '</a>');
}
else {
//using this to debug as have no idea how to run from script and use Logger.
  output.push(partAtts[0]+"<<< "+text.trim().indexOf('http://')+ ", "+ text.trim().indexOf('https://')+ " (pt) "+text+". "+indices);
  //output.push(text);
}
} 
else {
...

输出 -

<p>A sentence with a <a href="https://www.theguardian.com/politics/2017/oct/19/brexit-talks-uk-must-prepare-to-leave-without-deal-say-former-ministers" class="c">link</a></p>
<p>undefined<<< -1, -1 (pt) A full link sentence. 0</p>

这是 Google 文档中文本的样子。

任何帮助表示赞赏。真的超出了我的深度。即使它只是为了帮助我从脚本编辑器运行它。即选择一个文档,这样我就可以看到日志输出并增加我的试错输出!

【问题讨论】:

    标签: google-apps-script hyperlink google-docs


    【解决方案1】:

    我不明白你脚本的逻辑;它对 URL 和链接文本使用相同的“文本”变量。 Google 文档不应该在文本内容中包含像 http:// 这样的裸链接;链接被编码为其他文本属性,并通过getLinkUrl 访问。

    这是我的函数,它遍历所有文本元素、检测链接并返回 HTML 格式。请注意,一个文本元素可能包含多个链接。我的测试用例是

    带有linkanother link 以及更多文字的句子。

    A full link sentence

    输出是

    A sentence with a <a href="http://example.com">link</a> and <a href="https://stackoverflow.com">another link</a> and more text.
    <a href="http://example.com">A full link sentence</a>
    

    while 循环遍历文本元素;然后 for 循环遍历文本属性索引。 textPart 是两个索引之间的文本部分; url 是此部分链接到的任何内容(如果不是链接,则可能是 null)。每个部分都被推送到数组output,并在适用时使用链接格式。数组已加入并记录。

    function linkDetection() {
      var body = DocumentApp.getActiveDocument().getBody();
      var found = body.findElement(DocumentApp.ElementType.TEXT);
      while (found) {
        var elem = found.getElement();
        var text = elem.getText();
        var output = [];
        var indices = elem.getTextAttributeIndices();
        for (var i = 0; i < indices.length; i++) {
          var textPart = (i == indices.length - 1 ? text.slice(indices[i]) : text.slice(indices[i], indices[i+1]));      
          var url = elem.getLinkUrl(indices[i]);
          output.push(url ? '<a href="' + url + '">' + textPart + '</a>' : textPart);
        }
        Logger.log(output.join(''));
        found = body.findElement(DocumentApp.ElementType.TEXT, found);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-02-13
      • 2011-06-22
      • 1970-01-01
      • 2014-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-06
      • 1970-01-01
      相关资源
      最近更新 更多