英文:
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('• ', '\r\n');
}
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('•\\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.
答案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('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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论