Tuesday 13 February 2024

Replace text in a Google Doc with an image

The following Google Apps Script is designed to search the body of a Google Doc for a specific string/pattern (i.e. a keyword) and insert an image in place of it, optionally making it a clickable hyperlink too.


Instructions

In this example the code is designed to sit behind the Google Doc so it is bound to it. There are 4 pieces of information to complete in order to setup the script:

  1. searchText - this is the unique string/pattern in the Doc that you want to replace with an image e.g. "<<keyword>>"

  2. imageURL - this is the direct link to the image in Google Drive that you wish to use in the Doc.

  3. size - a numerical value representing the number of pixels for the image's width/height.

  4. hyperlinkURL - if you want the image to be clickable then provide a link for it.