在Google Docs中删除空行。

huangapple go评论73阅读模式
英文:

Removing empty lines in Google Docs

问题

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('• ', '\r\n');
}

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.

英文:

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 <br>
line 2 <br>
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(&#39;• &#39;, &#39;\r\n&#39;);
}

This adds a blank line between every line:
<br> line 1 <br>
<br>
line 2<br>
<br>
line 3 <br>
<br>
Apparently, there seems to be no way to remove all blank lines either.

答案1

得分: 1

换行符是问题所在。虽然 \n 会创建一个换行,但在Google Docs中可能不会被视为 项目符号列表 的正确分隔符。

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

以下正则表达式可以用于匹配每个项目符号并将其替换为换行,就像您手动点击了Enter键一样。

英文:

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(&#39;•\\s+&#39;, &#39;\n&#39;);
}

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.

答案2

得分: 1

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()
英文:

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(&#39;cmon.txt&#39;, encoding=&quot;ANSI&quot;) as reader, open(&#39;cmon.txt&#39;, &#39;r+&#39;, 
encoding=&quot;ANSI&quot;) as writer:
for line in reader:
    if line.strip():
        writer.write(line)
writer.truncate()

huangapple
  • 本文由 发表于 2023年5月14日 11:22:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76245673.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定