如何在文档中查找单词并插入网址 [英] How to find a Word in a Document and insert a url

查看:105
本文介绍了如何在文档中查找单词并插入网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在谷歌驱动器文档中的特定字词上插入超链接?



我可以找到这个词。之后,我想分配一个超链接。我使用这段代码:

  doc.editAsText()。findText(mot).setLinkUrl(https:// developers .google.com /应用程序脚本/ class_text); 

我的文档是DocumentApp,并且在使用UI时可以使用。但是,上面的代码不起作用。如何执行此任务?

解决方案

借助下面的实用程序功能,您可以执行此操作:

  linkText(mot,https://developers.google.com/apps-script/class_text); 

linkText()函数将查找所有在文档的文本元素中出现给定的字符串或正则表达式,并用URL包装找到的文本。如果 baseUrl %target%的形式包含占位符,则占位符将被替换为匹配的文本。

要从定制菜单中使用,您需要进一步包装实用功能,例如: $ b <$ p $找到当前文档中的所有ISBN编号,并添加一个url链接到他们,如果
*他们还没有。
*将此添加到您的自定义菜单中。
* /
函数linkISBNs(){
var isbnPattern =([0-9] {10}); //正则表达式格式为ISBN-10
linkText(isbnPattern,'http://www.isbnsearch.org/isbn/%target%');

$ / code>



代码



我最初编写这个实用程序函数来执行包含错误号的任务,并链接到我们的问题管理系统,但已将其修改为更通用。

  / ** 
*查找当前文档中目标文本的所有匹配项,并添加一个url
*链接到它们,如果它们还没有。 baseUrl可以
*包含一个占位符%target%,以替换为匹配的文本。
*
* @param {字符串}目标要搜索的文本或正则表达式。
*详细信息请参阅Body.findText()。
* @param {String} baseUrl应在匹配文本上设置的URL。
* /
函数linkText(target,baseUrl){
var doc = DocumentApp.getActiveDocument();
var bodyElement = DocumentApp.getActiveDocument()。getBody();
var searchResult = bodyElement.findText(target);

while(searchResult!== null){
var thisElement = searchResult.getElement();
var thisElementText = thisElement.asText();
var matchString = thisElementText.getText()
.substring(searchResult.getStartOffset(),searchResult.getEndOffsetInclusive()+ 1);
//Logger.log (matchString);

//如果找到的文本没有链接,请添加一个
if(thisElementText.getLinkUrl(searchResult.getStartOffset())== null){
// Logger .log('no link')
var url = baseUrl.replace('%target%',matchString)
//Logger.log(url);
thisElementText.setLinkUrl(searchResult.getStartOffset(),searchResult.getEndOffsetInclusive(),url);
}

//搜索下一个匹配
searchResult = bodyElement.findText(target,searchResult);
}
}


How can I insert a hyperlink on a particular word in a google-drive document?

I can find the word. After that, I want to assign a hyperlink. I used this code:

doc.editAsText().findText("mot").setLinkUrl("https://developers.google.com/apps-script/class_text");

My doc is DocumentApp, and this works when done using the UI. However, the code above does not work. How can I perform this task?

解决方案

With the help of the utility function below, you can do this:

linkText("mot","https://developers.google.com/apps-script/class_text");

The linkText() function will find all occurrences of the given string or regex in text elements of your document, and wrap the found text with a URL. If the baseUrl includes a placeholder in the form of %target%, the placeholder will be replaced with the matching text.

To use from a custom menu, you need to further wrap the utility function, for example:

/**
 * Find all ISBN numbers in current document, and add a url Link to them if
 * they don't already have one.
 * Add this to your custom menu.
 */
function linkISBNs() {
  var isbnPattern = "([0-9]{10})";  // regex pattern for ISBN-10
  linkText(isbnPattern,'http://www.isbnsearch.org/isbn/%target%');
}

Code

I originally wrote this utility function to perform the task of wrapping bug numbers with links to our issue management system, but have modified it to be more general-purpose.

/**
 * Find all matches of target text in current document, and add a url
 * Link to them if they don't already have one. The baseUrl may
 * include a placeholder, %target%, to be replaced with the matched text.
 *
 * @param {String} target   The text or regex to search for. 
 *                          See Body.findText() for details.
 * @param {String} baseUrl  The URL that should be set on matching text.
 */
function linkText(target,baseUrl) {
  var doc = DocumentApp.getActiveDocument();
  var bodyElement = DocumentApp.getActiveDocument().getBody();
  var searchResult = bodyElement.findText(target);

  while (searchResult !== null) {
    var thisElement = searchResult.getElement();
    var thisElementText = thisElement.asText();
    var matchString = thisElementText.getText()
          .substring(searchResult.getStartOffset(), searchResult.getEndOffsetInclusive()+1);
    //Logger.log(matchString);

    // if found text does not have a link already, add one
    if (thisElementText.getLinkUrl(searchResult.getStartOffset()) == null) {
      //Logger.log('no link')
      var url = baseUrl.replace('%target%',matchString)
      //Logger.log(url);
      thisElementText.setLinkUrl(searchResult.getStartOffset(), searchResult.getEndOffsetInclusive(), url);
    }

    // search for next match
    searchResult = bodyElement.findText(target, searchResult);
  }
}

这篇关于如何在文档中查找单词并插入网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆