2014-10-17 77 views
1

试图找出如何从Google文档(而非电子表格)中删除多个换行符。使用Google文档中的应用程序脚本删除换行符

我已经试过这和其许多变化:

function searchAndReplace() { 
    var bodyElement = DocumentApp.getActiveDocument().getBody(); 
    bodyElement.replaceText("\r\r", '\r'); 
} 

任何想法吗? 入门到这一切......

目的是为了复制搜索和在MS Word取代了^ P

回答

0

这里是一个比较“激进”的方法,如果您的文档仅具有文本段落(或图像其他元素将会丢失)。见doc about element types here

(代码中的注释)

function removeNewLines(){ 
    var doc = DocumentApp.getActiveDocument(); 
    var text = doc.getBody().getText();// get a string 
    var textMod=text.replace(/\n/g,'');// replace all \n with '' 
    Logger.log(textMod);//optional check in logger 
    doc.getBody().clear().appendParagraph(textMod);// empty the doc and apend new texr 
    doc.saveAndClose();// save the result 
} 
相关问题