skip to Main Content

I have some text in google docs.

• line 1• line 2• line 3

I would like to find an automated way to make this text

  • line 1
  • line 2
  • line 3

(with no empty lines between each bullet)

I used google apps script to filter out the bullet points (a special character in this case) in my text and add a new line with:

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

It seems to work but when I highlight all the text and click on bulleted list, only one bullet shows up on the first line. This is probably because ‘n’ is different than the enter key:

• line 1
line 2
line 3

What I want is to replace every bullet point with the same input one would get by deleting the bullet point and placing the enter key. This would mean that when I highlight the text after and click on bulleted list, I would get a bullet on each line.

I then tried changing my function to

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

This adds a blank line between every line:

line 1

line 2

line 3

Apparently, there seems to be no way to remove all blank lines either.

2

Answers


  1. Chosen as BEST ANSWER

    I ended up copying the google docs text into a text document (Make sure there is ANSI encoding on the .txt doc) and I used Python code to run through the file and remove the empty lines with this code:

    with open('cmon.txt', encoding="ANSI") as reader, open('cmon.txt', 'r+', 
    encoding="ANSI") as writer:
    for line in reader:
        if line.strip():
            writer.write(line)
    writer.truncate()
    

  2. The newline character is the issue. While n makes a newline, it may not be accepted as a proper separator in Google Docs for bulleted lists.

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

    The following regular expression may be used to match each bullet point and replace it with a line break as if you had manually clicked the Enter key.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search