Google Apps Script is a powerful tool that can help automate various tasks in Google Docs, Sheets, and Slides. It allows you to create custom functions, automate workflows, and even extend the functionality of Google products. In this post, we will focus specifically on how to use Google Apps Script to replace one word with another in Google Docs.

Step 1: Create a new Google Apps Script Project

To get started, you will need to create a new Google Apps Script project. Open a new or existing Google Doc, then navigate to Tools > Script editor. This will open a new tab in your browser with the Google Apps Script editor.

Step 2: Write the Script to Replace the Word

The script we will be writing will scan the entire Google Doc for a specific word and replace it with another word. Here is the code for the script:

function replaceWord() {
    var doc = DocumentApp.getActiveDocument();
    var searchFor = "word1";
    var replaceWith = "word2";
    var findText = doc.getBody().findText(searchFor);
    while (findText != null) {
        findText.getElement().asText().replaceText(searchFor, replaceWith);
        findText = doc.getBody().findText(searchFor, findText);

This script first gets the active Google Doc using `DocumentApp.getActiveDocument()`. It then sets the search term to look for and the word to replace it with.

The script then uses the `findText()` method to search the entire document for the word to replace. If the word is found, the script uses `getElement()` to select the text, then `replaceText()` to replace the word.

The script then uses `findText()` again, this time with the `startOffset` parameter set to the location of the last matched text, in order to find the next occurrence of the word to replace. This loop continues until all instances of the search term have been replaced.

Step 3: Test the Script

Now that you have written the script, it’s time to test it out. Go back to your Google Doc and make sure it contains the word you want to replace. Then, save the script and run it by clicking the Play button in the toolbar.

If everything is working correctly, the script should replace all instances of the word in the document with the new word. You can check the results by searching for the word using the standard Google Docs search feature.


One thing to keep in mind when using this script is that it will replace all instances of the search term, including instances within other words. For example, if you try to replace the word “cat” with “dog,” the script will also replace instances of “category” and “concatenate.” If you need to replace only exact matches, you will need to modify the script accordingly.

Another thing to keep in mind is that this script will only replace text in the main body of the Google Doc. If you have text in headers, footers, or comments, you will need to modify the script to search those portions of the document as well.