Eliminate newlines in google app script using regex

I'm trying to write part of an add-on for Google Docs that eliminates newlines within selected text using replaceText. The obvious text.replaceText("\n",""); gives the error Invalid argument: searchPattern. I get the same error with text.replaceText("\r","");. The following attempts do nothing: text.replaceText("/\n/","");, text.replaceText("/\r/","");. I don't know why Google App Script does not allow for the recognition of newlines in regex.

I am aware that there is an add-on that does this already, but I want to incorporate this function into my add-on.

This error occurs even with the basic

DocumentApp.getActiveDocument().getBody().textReplace("\n","");

My full function:

function removeLineBreaks() {

var selection = DocumentApp.getActiveDocument().getSelection();
if (selection) {
    var elements = selection.getRangeElements();
    for (var i = 0; i < elements.length; i++) {
        var element = elements[i];

        // Only deal with text elements

        if (element.getElement().editAsText) {
            var text = element.getElement().editAsText();

            if (element.isPartial()) {
                text.replaceText("\n","");
            }

            // Deal with fully selected text
            else {
                text.replaceText("\n","");
            }
        }
    }
}

// No text selected
else {
    DocumentApp.getUi().alert('No text selected. Please select some text and try again.');
}

}


Solution 1:

It seems that in replaceText, to remove soft returns entered with Shift-ENTER, you can use \v:

.replaceText("\\v+", "")

If you want to remove all "other" control characters (C0, DEL and C1 control codes), you may use

.replaceText("\\p{Cc}+", "")

Note that the \v pattern is a construct supported by JavaScript regex engine, and is considered to match a vertical tab character (≡ \013) by the RE2 regex library used in most Google products.

Solution 2:

The Google Apps Script function replaceText() still doesn't accept escape characters, but I was able to get around this by using getText(), then the generic JavaScript replace(), then setText():

var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();

var bodyText = body.getText();

//DocumentApp.getUi().alert( "Does document contain \\t? " + /\t/.test( bodyText ) ); // \n true, \r false, \t true

bodyText = bodyText.replace( /\n/g, "" );
bodyText = bodyText.replace( /\t/g, "" );

body.setText( bodyText );

This worked within a Doc. Not sure if the same is possible within a Sheet (and, even if it were, you'd probably have to run this once cell at a time).

Solution 3:

here is my pragmatic solution to eliminate newlines in Google Docs, or, more exact, to eliminate newlines from Gmail message.getPlainBody(). It looks that Google uses '\r\n\r\n' as a plain EOL and '\r\n' as a manuell Linefeed (Shift-Enter). The code should be self explainable. It might help to get alone with the newline problem in Docs. A solution possibly not very elegant, but works like a charm :-)

function GetEmails2Doc() { 
var doc = DocumentApp.getActiveDocument(); 
var body = doc.getBody(); 
var pc = 0;  // Paragraph Counter

var label = GmailApp.getUserLabelByName("_Send2Sheet"); 
var threads = label.getThreads(); 
var i = threads.length; 
// LOOP Messages within a THREAT  
for (i=threads.length-1; i>=0; i--) { 
for (var j = 0; j < messages.length; j++) { 
var message = messages[j]; 
/* Here I do some ...
body.insertParagraph(pc++, Utilities.formatDate(message.getDate(), "GMT",
"dd.MM.yyyy (HH:mm)")).setHeading(DocumentApp.ParagraphHeading.HEADING4) 
str = message.getFrom() + ' to: ' + message.getTo(); 
if (message.getCc().length >0) str = str + ", Cc: " + message.getCc(); 
if (message.getBcc().length >0) str = str + ", Bcc: " + message.getBcc(); 
body.insertParagraph(pc++,str);
*/ 
// Body !! 
var str = processBody(message.getPlainBody()).split("pEOL"); 
Logger.log(str.length + " EOLs"); 
for (var k=0; k<str.length; k++) body.insertParagraph(pc++,str[k]);
}
}
}

function processBody(tx) {

var s = tx.split(/\r\n\r\n/g);
// it looks like message.getPlainBody() [of mail] uses \r\n\r\n as EOL
// so, I first substitute the 'EOL's with the string pattern "pEOL"
// to be replaced with body.insertParagraph in the main function 
tx = ''; 
for (k=0; k<s.length; k++) tx = tx + s[k] + "pEOL"; 

// then replace all remaining simple \r\n with a blank 
s = tx.split(/\r\n/g); 
tx = ''; 
for (k=0; k<s.length; k++) tx = tx + s[k] + " ";

return tx;
}