Google Docs Replace With New Line

Google Docs Replace With New Line

Would you like to learn how to replace anything with a new line in Google Docs?

For example, you may have a paragraph full of names separated by commas, and you wish to replace each comma with a new line so that each name is on a new line.

Although there is no built-in function for this, you can create a function in Google Docs using App Script.

This article will guide you through the process of replacing all the commas (or anything else) with new lines in Google Docs, making your content tidier and more organized.

Google Docs Replace With New Line

Let’s dive right into the issue at hand.

You have a list of names, and they’re all jumbled together with commas, making it difficult to read or work with.

For instance, imagine having a list like this: Neil, LaNae, Harper, Camila, Elizabeth, Eleano

You want to put each name on a new line without using a comma, like this:

Neil
LaNae
Harper
Camila
Elizabeth
Eleano

So basically, all we have to do is replace all the “,” with a new line.

If you try to go to Edit > Find and Replace and experiment a bit, you’ll notice that there is no way to do that quickly.

Therefore, we will use the Apps Script feature to create our own tool to perform the action with ease.

Here’s a step-by-step guide on how to replace anything with a new line in Google Docs:

Step 1: Open the document containing the text

Google Docs Replace With New Line

The first step is to open the document containing the text that you want to edit.

Step 2: Go to Extensions > Apps Script

Google Docs Replace With New Line

After you open the document, click on the “Extensions” option in the top menu.

From the drop-down menu, select “Apps Script”.

Step 3: Delete all the existing code and replace it with the provided code

Google Docs Replace With New Line

Here, you’ll see the code editor where you can write your code to perform any action.

Now, delete all the lines from the given text editor and paste the following code:

function myFunction() {
  var doc = DocumentApp.getActiveDocument(); 
  var body = doc.getBody(); 
  body.replaceText(', ','\n');
}

Note: The above code will replace ‘, ‘ with a new line (\n). I have added a space after a comma in the code. If your text has no space after the comma, you can delete the space after the comma in the code. Or if you want to replace anything with a new line, simply enter that thing into the code.

This is like a recipe that tells Google Docs what to do. When you run this “recipe,” it does something specific to your document.

Breaking It Down:

  1. function myFunction() {: Think of this as the name of the recipe. It’s like saying, “Hey Google Docs, here are the instructions I want you to follow.”
  2. var doc = DocumentApp.getActiveDocument();: This line is like asking Google Docs to grab the document you’re working on. The “doc” variable is like a special holder for your document.
  3. var body = doc.getBody();: Imagine your document is a big book. This line tells Google Docs to open up the book and find the main part where all the words are. The “body” variable is like a bookmark that helps Google Docs know where to look.
  4. body.replaceText(‘, ‘,’\n’);: Here’s where the magic happens. The “body” knows where the words are in your document. This line tells Google Docs to look for any commas and replace them with something else. The , stands for the comma you want to find, and '\n' is a special thing that means “new line.” So, Google Docs will swap the commas for new lines, making things nice and neat.

Step 4: Give the project a name and save it

Google Docs Replace With New Line

By default, the project has no name.

Therefore, give the project a name that describes the action it performs so that you can easily identify it the next time you use it.

For instance, I named my project “Replace Comma With New Line”.

Finally, click the “Save” icon located just below the project name to save the project.

Step 5: Click on “Run”

Google Docs Replace With New Line

Once the project is ready, click on “Run” to execute the code.

Step 6: Click on “Review permissions”

Google Docs Replace With New Line

To allow Google to run this code, you need to grant permission.

When the pop-up appears, click “Review permissions”.

Step 7: Select your account

Google Docs Replace With New Line

Select the account from the list.

Make sure to choose the same account that you used to create the document in Google Docs.

Step 8: Click on “Advanced” and then click on “Go to Replace Comma With New Line (unsafe)”

Google Docs Replace With New Line

Google will now display a warning that the code is unsafe because it was not written by Google.

However, this is normal.

Click on the text “Advanced” and then click on the project name, for example, “Go to Replace Comma With New Line (unsafe)”.

Google Docs Replace With New Line

Step 9: Click on “Allow”

Google Docs Replace With New Line

Click the “Allow” button to enable the code to run and perform its action.

Step 10: Check the document to confirm the update

Google Docs Replace With New Line

Now, go to the Google Docs document tab where the text is located.

You will see that each comma has been replaced with a new line, and each name is now on a new line as a result.

You can modify the code to perform any actions you desire.

Google Docs Replace With New Line

Conclusion

Replacing anything with a new line in Google Docs is not possible with the built-in functions. However, by using the Apps Script feature, you can create a function to perform this action with ease.

To do this, the following steps need to be followed:

  1. Open the document containing the text.
  2. Go to Extensions > Apps Script.
  3. Delete all the existing code and replace it with the provided code.
  4. Give the project a name and save it.
  5. Click on “Run”.
  6. Click on “Review permissions”.
  7. Select your account.
  8. Click on “Advanced” and then click on “Go to Replace Comma With New Line (unsafe)”.
  9. Click on “Allow”.
  10. Check the document to confirm the update.

Author: Shubham Calmblay

Shubham Calmblay, founder of appsthatdeliver.com, has a decade of experience with various Google products. He has authored 1,000+ guides for ATD, published on prestigious tech blogs. His work has garnered recognition from Protocol.com, Leadsbridge.com, MadMobile.com, and numerous other leading publications and corporations.