Convert Soft Breaks to Hard Breaks on Google Documents using Google Apps Script

Gists

Description

This script converts soft breaks to hard breaks in a Google Document using Google Apps Script.

Usage

Follow these steps:

1. Create a New Google Document

Create a new Google Document and open it. Go to “View” -> “Show non-printing characters” in the top menu to see line breaks in the document body (as shown in the image below).

2. Sample Script

Copy and paste the following script into the script editor of your Google Document.

Important: Before using this script, enable the Google Docs API in Advanced Google services. Ref

function myFunction() {
  const doc = DocumentApp.getActiveDocument();
  const requests = [
    { replaceAllText: { replaceText: "\n", containsText: { text: "\u000b" } } },
  ];
  Docs.Documents.batchUpdate({ requests }, doc.getId());
}

Testing

Running the script on a sample document with soft breaks will convert them to hard breaks as follows.

Convert Soft Breaks to Hard Breaks on Google Documents using Google Apps Script

Note

References

 Share!