John
John

Reputation: 31

Google Apps Script to remove extra spaces from a Google Document

Google Documents don't allow extra spaces to be removed using search and replace like other text editors. Can anyone help with a Google Apps Script that searches a Document for all instances of two or more spaces and replaces them with single spaces?

Upvotes: 2

Views: 3983

Answers (2)

The Denster
The Denster

Reputation: 103

I got TypeError: Cannot read properties of null (reading 'getBody')

Upvotes: 0

user3717023
user3717023

Reputation:

Such a replacement can be done with the following one-line function:

function spaces() {  
  DocumentApp.getActiveDocument().getBody().replaceText(" +", " ");
}

Reference: replaceText method.

Upvotes: 6

Related Questions