2014-10-17 532 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 
} 
相關問題