简体   繁体   English

如何删除转换为字符串的文档的空白段落?

[英]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.

相关问题 如何删除段落中的额外行 - How to remove extra lines in paragraphs 如何在不弄乱MS Word 2013中的空白段落和对象标识符的情况下使用String.replace()方法? - How to use String.replace() method without messing up blank paragraphs and object identifiers in MS Word 2013? Javascript-从HTML字符串中删除空的段落 - Javascript - Remove empty paragraphs from HTML string 如何从JavaScript中删除字符串中的空白字符? - How would I remove blank characters from a string in JavaScript? JavaScript:如何使用正则表达式从字符串中删除空行? - JavaScript: how to use a regular expression to remove blank lines from a string? 当转换为字符串并输出时,“输入文本”字段会生成空白行 - Inout Text field generates a blank line when converted to string and output 删除无文字的段落 - Remove paragraphs with no text 如何从主题标签转换后的字符串中删除起始逗号 - ES6 - How do I remove starting comma from a hashtag converted string - ES6 如何扫描和删除UTF文本段落末尾的随机字符 - How to scan and remove random characters in the end of paragraphs in UTF text 如何选择末尾没有点的段落并删除-使用jQuery / javascript? - How to select paragraphs with no dot at the end and remove - with jQuery/javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM