How to Replace Text and Hyperlinks in Google Docs with Apps Script

0

The company manual is written in Google Docs. The document spans several pages and now the author has been asked to create links such that all mentions of the company name in the document point to the official website of the company.

It may take some time, but with Google Apps Script, specific words in a document can be linked in bulk with a single click.

This example shows how to find and replace all occurrences of a text phrase, the company name in this case, and add links to a specific website.

const addLinks = () => {
  const searchPhrase = 'Digital Inspiration';
  const hyperlink = 'https://digitalinspiration.com/';

  const document = DocumentApp.getActiveDocument();
  const body = document.getBody();
  let search = null;

  while ((search = body.findText(searchPhrase, search))) {
    const searchElement = search.getElement();
    const startIndex = search.getStartOffset();
    const endIndex = search.getEndOffsetInclusive();
    searchElement.asText().setLinkUrl(startIndex, endIndex, hyperlink);
  }

  document.saveAndClose();
};

For the next iteration of the manual, the company name has changed but the website domain is the same. The writer is required to change every instance of the company name in the document, but the underlying hyperlink must not be changed.

const changeText = () => {
  const searchText = 'Blue Widgets Inc.';
  const replaceText = 'Orange Inc.';

  const document = DocumentApp.getActiveDocument();
  const body = document.getBody();
  let search = null;

  while ((search = body.findText(searchText, search))) {
    const searchElement = search.getElement();
    const startIndex = search.getStartOffset();
    const endIndex = search.getEndOffsetInclusive();

    const textElement = searchElement.asText();
    const existingLink = textElement.getLinkUrl(startIndex);
    textElement.deleteText(startIndex, endIndex);
    textElement.insertText(startIndex, replaceText);
    textElement.setLinkUrl(startIndex, startIndex + replaceText.length - 1, existingLink);
  }

  document.saveAndClose();
};

The following Apps Script code snippets show how to change all instances of the business name and also change the site URL to another domain name.

const changeTextWithUrl = () => {
  const searchText = 'Blue Widgets Inc.';
  const replaceText = 'Orange Inc.';
  const replaceUrl = 'https://digitalinspiration.com/';

  const document = DocumentApp.getActiveDocument();
  const body = document.getBody();
  let search = null;

  while ((search = body.findText(searchText, search))) {
    const searchElement = search.getElement();
    const startIndex = search.getStartOffset();
    const endIndex = search.getEndOffsetInclusive();

    const textElement = searchElement.asText();
    textElement.deleteText(startIndex, endIndex);
    textElement.insertText(startIndex, replaceText);
    textElement.setLinkUrl(startIndex, startIndex + replaceText.length - 1, replaceUrl);
  }

  document.saveAndClose();
};
Share.

About Author

Comments are closed.