简体   繁体   English

如何使用脚本编辑器(谷歌文档插件)删除谷歌文档中的空行?

[英]How to remove empty lines in a google doc using the script editor (Google Docs add-ons)?

I am creating a Google Docs add-on, and one of the features I am trying to create is a feature that reduces the number of empty lines between two paragraphs.我正在创建一个 Google Docs 插件,我正在尝试创建的功能之一是减少两个段落之间的空行数量的功能。

So, for instance, if I have 2 paragraphs with 5 empty/blank lines between them, I want the feature to reduce the number of empty lines to 1.因此,例如,如果我有 2 个段落,它们之间有 5 个空行/空行,我希望该功能将空行数减少到 1。

Essentially, I need a way to detect an empty line.本质上,我需要一种检测空行的方法。 I've looked at the API , and think I need to use the replaceText() method which searches for a regex pattern.我查看了API ,并认为我需要使用搜索正则表达式模式的 replaceText() 方法。 However, I've tried it, and it didn't work (perhaps I'm using an incorrect patter, i dont know).但是,我已经尝试过了,但它没有用(也许我使用了不正确的模式,我不知道)。

Can anyone help me in finding a way to detect an empty line?谁能帮我找到一种检测空行的方法? Thanks.谢谢。

EDIT:编辑:

I just found out that Google Docs don't support all patterns of regex.我刚刚发现 Google Docs 并不支持所有的正则表达式模式。 Here is that link: https://support.google.com/analytics/answer/1034324?hl=en .这是该链接: https://support.google.com/analytics/answer/1034324?hl=en I'm not familiar with regex.我不熟悉正则表达式。 Can anyone provide an alternative that works in Google Docs.任何人都可以提供适用于 Google Docs 的替代方案。

Written and tested the below function, here are the steps to make it work编写并测试了以下 function,以下是使其工作的步骤

  1. Copy the text from here Lorem Ipsum Google Doc , to a new Google Doc将文本从Lorem Ipsum Google Doc复制到新的 Google Doc

  2. Add the below snippet to your Tools > Script editor > project将以下代码段添加到您的工具 > 脚本编辑器 > 项目

  3. Comments inline内联评论

// Regular expressions with the following special characters are not supported, 
// as they can cause delays in processing your email: * (asterisk), + (plus sign)
// So RegEx is not applicable since you can't use "\s*", we need to find another solution

// A row/line is a Paragraph, weird right? So now you iterate through the rows
// Trim paragraph (row), if it's empty, then you can delete it.

function removeEmptyLines() {
  var doc = DocumentApp.getActiveDocument();
  Logger.log("Before:\n", doc.getBody().getText());  
  
  var paragraphs = doc.getBody().getParagraphs();
  // Iterating from the last paragraph to the first
  for (var i=paragraphs.length-1; i>=0; i--){
    var line = paragraphs[i];
    if ( ! line.getText().trim() ) {
      // Paragraph (line) is empty, remove it
      line.removeFromParent()
      Logger.log("Removed: ", i);
    }
  }
  Logger.log("After:\n", doc.getBody().getText());
}
References 参考
  1. https://developers.google.com/apps-script/reference/document/text#replaceText(String,String) https://developers.google.com/apps-script/reference/document/text#replaceText(String,String)
  2. https://support.google.com/a/answer/1371415?hl=en https://support.google.com/a/answer/1371415?hl=en
  3. https://stackoverflow.com/a/3012832/5285732 https://stackoverflow.com/a/3012832/5285732

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WooCommerce Google 表格中的附加组件信息 - WooCommerce Add-ons information in Google Sheets 正在寻找一种使用 Google Apps 脚本或 DOCS API 为 Google 文档中的段落添加边框的方法? - Looking for a way to add borders to a paragraph in a google doc using a Google Apps Script or perhaps the DOCS API? Codemirror,如何添加加载项 - Codemirror, how to add add-ons 使用 Javascript 在 Firefox 中禁用加载项 - Disable add-ons in firefox using Javascript Chartjs 附加组件 - Chartjs Add-Ons 如何在现有脚本上添加Google文档链接 - How to add a Google Doc link on an existing script 如何使用谷歌应用脚本在谷歌文档中获取字符串的索引 - How to get the index of a string in google docs using google App Script 在Google文档的侧边栏中重新加载div,然后使用应用脚本添加 - Reload a div in sidebar of google docs add on using app script 无需夜间构建即可将ECMAScript 6与Firefox插件SDK结合使用 - Using ECMAScript 6 with Firefox add-ons SDK without nightly builds 是否有任何选项可以在 javascript 中的 Google 表格脚本或其他程序中编写代码,以对附加组件工具执行一些操作? - Is there any option to write a code in javascript on Google Sheets scripts or in others program that will do some action on Add-ons tool?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM