简体   繁体   中英

Retrieve the hyperlink on a specific text string within Google Doc using Apps Script

I'm trying to use Google Apps Script to get the hyperlink from a specific string found in this Google Doc .

The string is ||stock||

The hyperlink is https://www.cnbc.com/quotes/?symbol=aapl&qsearchterm=aapl

Any help is greatly appreciated.

The code I'm currently using

function docReport() {
  var doc = DocumentApp.openByUrl('https://docs.google.com/document/d/1XNiqgJ_hM2SWjoR-OTsq1w-ZFKvTIERDIs_NOWJpckY/edit');
  var body = doc.getBody();
  Logger.log(body.getParagraphs().length);//get the number of paragraphs
  //https://www.udemy.com/apps-script-course/learn/v4/t/lecture/10208226?start=0
  for (var x=0;x<body.getParagraphs();X++) {
   var el = body.getChild(x);
    Logger.log(el.getText());
  }


  var bodyText = body.getText();
  var words = bodyText.match(/\S+/g); // get word count for body - https://stackoverflow.com/questions/33338667/function-for-word-count-in-google-docs-apps-script
  Logger.log(words.length); // retruns # of words

  var paragraphAll = body.getParagraphs(); // gets all paragraph objects in a document
  Logger.log(paragraphAll); 

  var paragraphText = paragraphAll[1].getText().match(/\S+/g);
  Logger.log(paragraphText.length); // retruns # of words in a paragraph




}
  • You want to retrieve hyperlink of the text of ||stock||.

If my understanding is correct, for example, how about this sample script? In your situation, the text value which has a link has already been known. The sample script uses this situation.

By the way, from your question, I'm not sure whether there are several values of ||stock||in the document. So this sample script supposes that there are several values of ||stock||in the document.

I think that there are several answers for your situation. So please think of this as one of them.

Sample script:

var searchValue = "\\|\\|stock\\|\\|"; // Search value
var body = DocumentApp.openByUrl('https://docs.google.com/document/d/1XNiqgJ_hM2SWjoR-OTsq1w-ZFKvTIERDIs_NOWJpckY/edit').getBody();
var searchedText = body.findText(searchValue);
var urls = [];
while (searchedText) {
  var url = searchedText.getElement().asText().getLinkUrl(searchedText.getStartOffset());
  urls.push(url);
  searchedText = body.findText(searchValue, searchedText);
}
Logger.log(urls) // Results

Note:

If there is only one search value in the document, you can also use the following script.

var searchValue = "\\|\\|stock\\|\\|";
var body = DocumentApp.openByUrl('https://docs.google.com/document/d/1XNiqgJ_hM2SWjoR-OTsq1w-ZFKvTIERDIs_NOWJpckY/edit').getBody();
var searchedText = body.findText(searchValue);
var url = searchedText.getElement().asText().getLinkUrl(searchedText.getStartOffset());
Logger.log(url)

References:

If I misunderstand your question, please tell me. I would like to modify it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM