Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 在Google文档中使用应用程序脚本删除换行符_Google Apps Script_Google Docs - Fatal编程技术网

Google apps script 在Google文档中使用应用程序脚本删除换行符

Google apps script 在Google文档中使用应用程序脚本删除换行符,google-apps-script,google-docs,Google Apps Script,Google Docs,正在尝试解决如何从Google文档(而不是电子表格)中删除多个换行符 我已经尝试过这个和它的许多变体: function searchAndReplace() { var bodyElement = DocumentApp.getActiveDocument().getBody(); bodyElement.replaceText("\r\r", '\r'); } 有什么想法吗? 这一切都没有意义 其目的是在MS Word中复制搜索并替换^p如果您的文档只有带有文本的段落(图像或其他元

正在尝试解决如何从Google文档(而不是电子表格)中删除多个换行符

我已经尝试过这个和它的许多变体:

function searchAndReplace() {
  var bodyElement = DocumentApp.getActiveDocument().getBody();
  bodyElement.replaceText("\r\r", '\r');
}
有什么想法吗? 这一切都没有意义

其目的是在MS Word中复制搜索并替换^p

如果您的文档只有带有文本的段落(图像或其他元素将丢失),则这里有一种相当“激进”的方法。看

(代码注释)


我想做同样的事情(用一个新行替换两个新行)。由于某些原因,replaceText()不接受以下内容\n

function myFunction() {
  var body = DocumentApp.getActiveDocument().getBody();
  var text = body.editAsText();
  var text_content = text.getText();
  for(var i = 0, offset_i = 0; i < (text_content.length); i++){    
    if((text_content.charCodeAt(i)==10) && (text_content.charCodeAt(i-1)==10)){
      text.deleteText(i-1-offset_i, i-1-offset_i)
      offset_i++;
    }

  }

}
函数myFunction(){
var body=DocumentApp.getActiveDocument().getBody();
var text=body.editAsText();
var text_content=text.getText();
对于(变量i=0,偏移量i=0;i<(text\u content.length);i++{
if((text_content.charCodeAt(i)==10)和&(text_content.charCodeAt(i-1)==10)){
text.deleteText(i-1-offset\u i,i-1-offset\u i)
偏移量i++;
}
}
}
function myFunction() {
  var body = DocumentApp.getActiveDocument().getBody();
  var text = body.editAsText();
  var text_content = text.getText();
  for(var i = 0, offset_i = 0; i < (text_content.length); i++){    
    if((text_content.charCodeAt(i)==10) && (text_content.charCodeAt(i-1)==10)){
      text.deleteText(i-1-offset_i, i-1-offset_i)
      offset_i++;
    }

  }

}