How to add a hyperlink in a Google Docs using a Google Script
Asked Answered
E

5

22

I have always used the insertText() function, but now I want to write a link in my google docs. The ideal would be to be able to write in HTML, but I don't know how.. it seems that it is not possible with the insertText() function.

How can I do that ?

Extensible answered 16/9, 2015 at 7:39 Comment(2)
You should add more information what you tried already to solve that problem.Larimore
#69142576Promissory
D
31

You should be able to use setFormula and the Hyperlink formula like so:

var value = '=HYPERLINK("www.google.com", "Google")';

SpreadsheetApp.getActiveSpreadsheet()
   .getSheetByName("Sheet1")
   .getRange("A1")
   .setFormula(value);

Edit: Looks like I misread the question. Try this instead:

DocumentApp.getActiveDocument()
  .getBody()
  .editAsText()
  .insertText(0, "link text")
  .setLinkUrl("www.google.com");

Edit 2: Looks like .setLinkUrl() is effecting the whole body, not the text inserted. If you put the link text into a variable and use the length of the variable to mark the link area, it should work. Try this instead:

function insertLink() {
  var text = "link text\n";
  var url = "www.google.com";
  DocumentApp.getActiveDocument()
    .getBody()
    .editAsText()
    .insertText(0, text)
    .setLinkUrl(0, text.length, url);
}
Disproof answered 21/9, 2015 at 9:42 Comment(4)
Thank you but I am looking for a way to do that in Google Docs, not Google SpreadsheatExtensible
I'm sorry, you're right. Try adding .setLinkUrl("www.google.com"); after .insertText() with your link text instead of google.com. E.G. DocumentApp.getActiveDocument().getBody().editAsText().insertText(0, "link text").setLinkUrl("www.google.com");Disproof
Thank you very much for your answer. I tried this, but unfortunately, the whole document (the new line at the beginning + the rest of the doc already there) become a link with this method... What can I do to solve that ?Extensible
@Extensible see my latest edit, that should solve it I think.Disproof
T
9

To add a hyperlink in a document use Body.appendParagraph with setLinkUrl, then merge.

let doc = DocumentApp.create("My Document");
let body = doc.getBody();
body.appendParagraph("Please click ");
let link = body.appendParagraph("here").setLinkUrl("http://www.google.com");
link.merge();
let closing = body.appendParagraph(".");
closing.merge();

The code above will create a document with text that looks like:

Please click here.

Trilingual answered 21/10, 2020 at 22:43 Comment(3)
this answer is much cleaner than the accepted one.Garfieldgarfinkel
Thanks for this. How can we do this for a text in Google Slides?Downer
@Downer See #63552231Trilingual
E
3

You can also use the function below to make all the links in your document clickable.

    function makeLinksClickable(document) {
        const URL_PATTERN="http[^\b]+"
        const body = document.getBody()
        var foundElement = body.findText(URL_PATTERN);
        while (foundElement != null) {
          // Get the text object from the element
          var foundText = foundElement.getElement().asText();
          
          // Where in the element is the found text?
          const start = foundElement.getStartOffset();
          const end = foundElement.getEndOffsetInclusive();
          const url = foundText.getText().substring(start,end + 1)      
          //make url clickable
          foundText.setLinkUrl(start, end, url)
          
          // Find the next match
          foundElement = body.findText(URL_PATTERN, foundElement);
        }    
      }

Endowment answered 25/3, 2021 at 15:30 Comment(0)
R
0

If you are looking to find a string with a hyperlink, the following code will work.

function insertLink(){      
      const body = DocumentApp.getActiveDocument().getBody()
      const text = body.findText('{{googleLink}}').getElement().asText()  
      text.setText('Link to google')
      text.setLinkUrl('www.google.com')
}
Retrospect answered 13/1, 2023 at 22:10 Comment(0)
J
-2

I am using this script, this is working Calomun 1 Row > 2.

    function InsertLink(e)
    {
      var actSht = e.source.getActiveSheet();
      if (actSht.getName() == ['SheetName']){

      var activeCell = actSht.getActiveCell(); //Detec the ActiveCell

      //var activeCell = event.range;
      var activeCellValue = e.value;

      var column = activeCell.getColumn();
      var colNums  = [1]; //Columns, whose edit is considered
      if(colNums.indexOf(column) == -1) return; //If column other than considered then return

      var row = activeCell.getRow();
      if(row < 2)   return; //If header row then return

      var length = String(activeCellValue).length;

      if (!e.value)
      {
        activeCell.setValue()
      }
      else if(length > 4)
      {
        activeCell.setValue('=HYPERLINK' + '("http://otrs/otrs/index.pl?Action=AgentTicketZoom;TicketNumber='+activeCellValue+'";"'+activeCellValue+'")'        );
      }
    }
    }
Jenaejenda answered 27/11, 2016 at 0:32 Comment(1)
This question is about Google Docs, not Google Sheets.Akbar

© 2022 - 2024 — McMap. All rights reserved.