Introduction

This blog explains how to quickly remove all hyperlinks in Google Docs. Hyperlinks often get automatically added when you paste content from websites, emails or documents. While useful in some cases, they can clutter up your text or distract readers in formal reports, school assignments or printed handouts. In Microsoft Office, there is a feature that allows you to remove all hyperlinks at once with a shortcut. If you're looking for a clean, link-free document, this guide will walk you through simple methods, manual and automated to remove all hyperlinks at once in Google Docs, saving time and effort.

Method 1: Use Office Word

This is an workaround, but you could

  1. Export the Doc to Word .docx by File > Download as > Microsoft Word.
  2. Open the .docx. Select the text with hyperlinks.
  3. Press shortcut Ctrl + Shift + F9.
  4. The hyperlinks will go away.
  5. Clean the hyperlink underlines with Format Painter.

Method 2: Use Apps script

This method is more technical and works well.

  1. Copy the apps script to your Docs script editor with a name Remove all hyperlinks. If you don't know how to do it, here is a tutorial how to add an apps script to your Docs.
  2. Select the text in which you want to remove all hyperlinks. It doesn't have to be the entire document.
  3. Click menu Add-ons > Remove all hyperlinks > Remove all hyperlinks.
  4. The hyperlinks shall be removed while the format is kept.
Remove all hyperlinks in Docs with the Apps script below
Remove all hyperlinks in Docs with the Apps script below
/**
 * @license MIT
 *
 * © 2020 xfanatical.com. All Rights Reserved.
 * @since 1.0.0 remove links
 * @since 1.0.1 fixed a typo 
 */
 
function onOpen() {
  DocumentApp.getUi()
    .createAddonMenu()
    .addItem('Remove all hyperlinks', 'init')
    .addToUi()
}

/**
 * Get an array of all LinkUrls in the element
 * @param {Element} element The document element to operate on. 
 * @returns {Array}  Array of objects,
 * {
 *   element,
 *   startOffset,
 *   endOffsetInclusive, 
 *   url
 * }
 * credit to https://gist.github.com/mogsdad/6518632
 */
function getAllLinks(element, selectionStartOffset, selectionEndOffsetInclusive) {
  var links = []

  var type = element.getType()
  if (type === DocumentApp.ElementType.TEXT) {
    var textObj = element.editAsText()
    var text = element.getText()
    var inUrl = false
    
    var firstStartOffset = 0
    if (selectionStartOffset !== -1) {
      firstStartOffset = selectionStartOffset
    }
    
    var lastEndOffsetInclusive = text.length
    if (selectionEndOffsetInclusive !== -1) {
      lastEndOffsetInclusive = selectionEndOffsetInclusive + 1
    }
    
    for (var ch = firstStartOffset; ch < lastEndOffsetInclusive; ch++) {
      var url = textObj.getLinkUrl(ch)
      if (url != null) {
        if (!inUrl) {
          // We are now!
          inUrl = true
          var curUrl = {}
          curUrl.element = element
          curUrl.url = url
          curUrl.startOffset = ch
        } else {
          curUrl.endOffsetInclusive = ch
          if (ch === text.length - 1) {
            // this hyperlink is the end of the TEXT element
            inUrl = false
            links.push(curUrl)
            curUrl = {}
          }
        }          
      } else {
        if (inUrl) {
          // Not any more, we're not.
          inUrl = false
          links.push(curUrl)  // add to links
          curUrl = {}
        }
      }
    }
  }
  
  var singletonElement
  if (type === DocumentApp.ElementType.INLINE_IMAGE) {
    singletonElement = element.asInlineImage()
  } else if (type === DocumentApp.ElementType.EQUATION) {
    singletonElement = element.asEquation()
  } else if (type === DocumentApp.ElementType.EQUATION_FUNCTION) {
    singletonElement = element.asEquationFunction()
  } else if (type === DocumentApp.ElementType.LIST_ITEM) {
    singletonElement = element.asListItem()
  } else if (type === DocumentApp.ElementType.PARAGRAPH) {
    singletonElement = element.asParagraph()
  } else if (type === DocumentApp.ElementType.TABLE) {
    singletonElement = element.asTable()
  } else if (type === DocumentApp.ElementType.TABLE_CELL) {
    singletonElement = element.asTableCell()
  } else if (type === DocumentApp.ElementType.TABLE_OF_CONTENTS) {
    singletonElement = element.asTableOfContents()
  } else if (type === DocumentApp.ElementType.TABLE_ROW) {
    singletonElement = element.asTableRow()
  } else if (type === DocumentApp.ElementType.TEXT) {
    singletonElement = element.asText()
  }
  
  if (singletonElement) {
    var link = singletonElement.getLinkUrl()
    if (link) {
      links.push({
        element: singletonElement,
        url: link,
      })
    }
  }

  if (element.getNumChildren) {
    var numChildren = element.getNumChildren()
    for (var i = 0; i < numChildren; i++) {
      links = links.concat(getAllLinks(element.getChild(i), selectionStartOffset, selectionEndOffsetInclusive))
    }
  }

  return links
}

function init() {
  var doc = DocumentApp.getActiveDocument()
  var selection = doc.getSelection()
  if (selection) {
    var elements = selection.getRangeElements()
    for (var i = 0; i < elements.length; i++) {
      var rangeElement = elements[i]
      var element = rangeElement.getElement()
      
      if (element.editAsText) {
        var text = element.editAsText()
        
        var links = getAllLinks(element, rangeElement.getStartOffset(), rangeElement.getEndOffsetInclusive())
        for (var j = 0; j < links.length; j++) {
          var link = links[j]
          if (link.element.setLinkUrl) {
            if (link.hasOwnProperty('startOffset')) {
              link.element.setLinkUrl(link.startOffset, link.endOffsetInclusive, '')
            } else {
              link.element.setLinkUrl(null)
            }
          }
        }
      }
      
    }
  } else {
    DocumentApp.getUi().alert('Select the text with hyperlinks to be removed')
  }
}

Method 3: Use Add-on

If you’ve landed here, you’re in luck. There's a free Google Docs add-on called Text Cleaner that can quickly remove all hyperlinks and underlining from your document. It's a more convenient and hassle-free option. The steps are almost identical to Method 2, except you’ll use the menu Text Cleaner > Remove links and underlining.

Remove all hyperlinks with add-on Text Cleaner
Remove all hyperlinks with add-on Text Cleaner

Hope it works for you. Feel free to leave any comments. Thanks for reading.

Conclusion

Removing hyperlinks in Google Docs doesn’t have to be a tedious, link-by-link task. Whether you prefer using keyboard shortcuts, built-in Docs features or a free add-on like Text Cleaner, you have multiple ways to clean up your document quickly. Choose the method that best fits your workflow and enjoy a distraction-free, polished document ready for sharing, printing or presenting.

Related Articles

For More articles visit our website: xFanatical Articles