检查段落的一部分是否在 Google 文档中被超链接

check if portion of paragraph is hyperlinked in Google doc

这就是我 select 我在 Google 文档中的段落的方式

let document = DocumentApp.getActiveDocument().getBody();
let paragraphs = document.getParagraphs();
let paragraph = paragraphs[5];

现在如何检查 URL 链接到 index/position startend 的段落部分?

例如,如果段落是

你好黑暗我的old friend

那么对于位置 19 到 23 应该 return 正确,因为那部分文本链接到 Whosebug。

有方法 paragraph.editAsText().getLinkUrl() 但它没有将 indices/positions 作为参数。

请帮忙

试试这个:

function findURL() {
  var document = DocumentApp.getActiveDocument().getBody();
  var paragraphs = document.getParagraphs();
  var text = paragraphs[0].getText();
  var child = paragraphs[0].getChild(0).asText();
  //Storing words in an array
  var words = text.match(/\S+/g);
  //check if array is empty
  if (words) {
    //iterate each words
    words.forEach(word => {
      //use findtext and getStartOffset to find the start position of string
      //use getLinkUrl(offset) to check if the word has link on it.
      if(child.getLinkUrl(child.findText(word).getStartOffset())){
        //print the position of words that has link.
        //added +1 since the start of count in getStartOffset() is zero.
        Logger.log(child.findText(word).getStartOffset()+1);
      }
    })
  }
}

示例:

输出:

参考文献: