---
title: "Replace text in a Google Doc with an image from Google Drive with Google Apps Script"
author: "Phil Bainbridge"
date: 2024-02-19T20:20:09+00:00
modified: 2024-02-19T20:20:55+00:00
canonical_url: https://pulse.appsscript.info/p/2024/02/replace-text-in-a-google-doc-with-an-image/
markdown_url: https://pulse.appsscript.info/p/2024/02/replace-text-in-a-google-doc-with-an-image.md
categories:
  - "Beginner"
  - "Docs"
  - "Drive"
  - "Snippet"
tags:
  - "pbainbridge.co.uk"
featured_image: https://pulse.appsscript.info/files/2024/02/replace-image.jpg
description: "Latest little snippet contributed by Phil Bainbridge to AppsScriptPulse: \"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.\" ... I was surprised there is no `DriveApp.getFileByUrl()` method, which means Phil had to make an offering to the RegEx gods (feature request is here https://issuetracker.google.com/issues/36761537 )."
---

# Replace text in a Google Doc with an image from Google Drive with Google Apps Script

![Replace text in a Google Doc with an image from Google Drive with Google Apps Script](https://pulse.appsscript.info/files/2024/02/replace-image.jpg)

> Search the body of a Google Doc for a specific string/pattern and insert an image in place of it.

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.

Source: *[The Gift of Script: Replace text in a Google Doc with an image*](https://www.pbainbridge.co.uk/2024/02/replace-text-in-google-doc-with-image.html)

---

*Published by [Phil Bainbridge](https://pulse.appsscript.info/p/author/phillipbainbridge/) on [Pulse](https://pulse.appsscript.info/). Original article: [https://pulse.appsscript.info/p/2024/02/replace-text-in-a-google-doc-with-an-image/](https://pulse.appsscript.info/p/2024/02/replace-text-in-a-google-doc-with-an-image/)*
