Creating Hyperlinks from Selected Text in Google Docs Scripts

Hey everyone! I’m trying to figure out how to make a Google Docs script that turns selected text into a hyperlink. I want users to highlight a name and have it automatically become a link to a website with that name in the URL.

For example, if someone selects ‘Jane Doe’, it should turn into a link like ‘https://namesite.com/jane-doe’.

I’ve tried this basic code, but it’s not working:

function makeLink() {
  var doc = DocumentApp.getActiveDocument();
  var selection = doc.getSelection();
  
  if (selection) {
    var text = selection.getRangeElements()[0].getElement().asText();
    text.setLinkUrl('https://namesite.com/example');
  }
  
  DocumentApp.getUi().alert('Link created!');
}

Any ideas on how to get this working and how to convert the selected text into a proper URL slug? Thanks for any help!

I’ve worked on a similar script before. One thing to consider is handling special characters and accents in names. You might want to use a library like ‘slug’ to create URL-friendly strings. Here’s a modified version of your script:

function makeLink() {
  var doc = DocumentApp.getActiveDocument();
  var selection = doc.getSelection();
  
  if (selection) {
    var element = selection.getRangeElements()[0].getElement();
    if (element.editAsText) {
      var text = element.editAsText();
      var selectedText = text.getText().substring(text.getStartOffset(), text.getEndOffsetInclusive() + 1);
      var urlSlug = createSlug(selectedText);
      text.setLinkUrl('https://namesite.com/' + urlSlug);
      DocumentApp.getUi().alert('Link created successfully!');
    }
  } else {
    DocumentApp.getUi().alert('No text selected. Please highlight some text and try again.');
  }
}

function createSlug(str) {
  return str.toLowerCase()
    .replace(/[àáâãäå]/g, 'a')
    .replace(/[èéêë]/g, 'e')
    .replace(/[ìíîï]/g, 'i')
    .replace(/[òóôõö]/g, 'o')
    .replace(/[ùúûü]/g, 'u')
    .replace(/[^a-z0-9]+/g, '-')
    .replace(/^-+|-+$/g, '');
}

This should handle most cases, including names with accents or special characters. Remember to test thoroughly with various inputs.

hey there! I’ve dealt with this before. ur code’s close, but u need to grab the selected text and format it for the URL. try something like:

var selectedText = text.getText();
var urlSlug = selectedText.toLowerCase().replace(/ /g, '-');
text.setLinkUrl('https://namesite.com/' + urlSlug);

this should do the trick. good luck!

I’ve actually implemented something similar in one of my projects. Your approach is on the right track, but there are a few modifications that made my solution more robust. Instead of simply replacing spaces, I found it useful to remove non-alphanumeric characters using regular expressions, convert the text to lowercase, and also handle the scenario where no text is selected.

Here is an updated version of your script:

function makeLink() {
  var doc = DocumentApp.getActiveDocument();
  var selection = doc.getSelection();
  
  if (selection) {
    var element = selection.getRangeElements()[0].getElement();
    if (element.editAsText) {
      var text = element.editAsText();
      var selectedText = text.getText().substring(text.getStartOffset(), text.getEndOffsetInclusive() + 1);
      var urlSlug = selectedText.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '');
      text.setLinkUrl('https://namesite.com/' + urlSlug);
      DocumentApp.getUi().alert('Link created!');
    }
  } else {
    DocumentApp.getUi().alert('Please select some text first.');
  }
}

This approach should cover most edge cases effectively. Let me know if you encounter any further issues.