11

I am migrating documents to Google Docs that contain many URLs. If I hit Enter after each one, the link becomes clickable. But I'm looking for a way to convert all URLs (text starting with http:// or https://) to be clickable.

Is there a macro, add-on or script I could run?

0

3 Answers 3

16

I did some research on using Google Apps Scripts and I came up with this script that works for my needs. I hope it's useful for someone else too.

function onOpen() {
  DocumentApp.getUi().createAddonMenu()
    .addItem('Make URLs Clickable', 'makeUrlsClickable')
    .addToUi();
}

function makeUrlsClickable() {
  var urlRegex = 'http[s]?:\/\/[^ ]+';
  var body = DocumentApp.getActiveDocument().getBody();
  var urlElement = body.findText(urlRegex);  

  while (urlElement != null) {    
    var urlText = urlElement.getElement().asText();

    var startOffset = urlElement.getStartOffset();
    var endOffset = urlElement.getEndOffsetInclusive();

    urlText.setLinkUrl(startOffset, endOffset, getOnlyUrl(urlText.getText()));

    urlElement = body.findText(urlRegex, urlElement);
  }
}

function getOnlyUrl(text) {
  var startOffset = text.indexOf('http');
  var endOffset = text.indexOf(' ', startOffset);

  if (endOffset === -1) {
    endOffset = text.length;
  }

  return text.substring(startOffset, endOffset);
}    
0
2

Copy and paste links which are already formatted as hyperlinks.

Here's one easy way to do this:

  1. Paste your content into a site such as Convert Plain Text URLs to Link (or even a Stack Exchange question or answer draft).
  2. Copy the output
  3. Paste into your document (don't hold shift)
  4. Remove formatting (this keeps the hyperlink but makes your font match)

This works in both Docs and Sheets, and probably others that I didn't test.

-2

I followed this tutorial: YouTube Link

To summarize it's content: it used Start function from Power Tools Add-On. As you will see in the video and in your sheet, if you edit the cell having a URL and write one space more after the link and then finalize your cell editing, the link appears as clickable. So the function used makes this 'modification' in a bulk approach. For me worked really well.

Not the answer you're looking for? Browse other questions tagged or ask your own question.