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("
","");
gives the error Invalid argument: searchPattern
. I get the same error with text.replaceText("
","");
. The following attempts do nothing: text.replaceText("/
/","");
, text.replaceText("/
/","");
. 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("
","");
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("
","");
}
// Deal with fully selected text
else {
text.replaceText("
","");
}
}
}
}
// No text selected
else {
DocumentApp.getUi().alert('No text selected. Please select some text and try again.');
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…