[英]How to remove blank paragraphs of a document converted in string?
I have a very large string, which I get using this GAS methods: var myDocText = DocumentApp.openById(docId).getBody().copy().getText();
我有一个很大的字符串,可以使用以下GAS方法获得:
var myDocText = DocumentApp.openById(docId).getBody().copy().getText();
(the getText()
method "Retrieves the contents of the element as a text string.") (
getText()
方法“以文本字符串检索元素的内容。”)
And I want to remove the caracters corresponding to the blank paragraphs and new lines of that document converted in a string. 我想删除与空白段落相对应的字符,以及该文档中以字符串转换的新行。 How can I do that?
我怎样才能做到这一点?
I'm using this GAS body method (which reduces, but doesn't delete all blank lines): 我正在使用此GAS正文方法(该方法会减少,但不会删除所有空白行):
var myText = DocumentApp.openById(docId).getBody();
for (var c = 0; c < myText.getNumChildren(); c++){
var child = myText.getChild(c);
if ( (child.getType() == DocumentApp.ElementType.PARAGRAPH) && ( (child.getText() == '') || (child.getText() == ' ') ) ) {
myText.removeChild(child);
}
}
But I'd like to use a string solution. 但我想使用字符串解决方案。 I've tried
.replace(/^\\s*[\\r\\n]/gm)
; 我已经尝试过
.replace(/^\\s*[\\r\\n]/gm)
; and it didn't work`. 而且没有用`。 Any suggestion?
有什么建议吗?
我会做
myText.split(/(\r\n|\n|\r)/gm).join('');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.