Instead of using spaces consider to useusing non-breaking spaces \xa0
instead.
The following code uses the String.prototype.replaceString.prototype.replace
and with a regular expression / /g
* to replace all the spaces bywith non-breaking spaces.
* Global flag g
is required or it'll only replace the 1st space.
function myFunction() {
const message = 'foo bar'.replace(/ /g,'\xa0');
SpreadsheetApp.getUi().alert(message);
}
If using this repeatedly in your script, you could also create a function and call it where needed:
function myFunction() {
const message = noBreakSpaces('foo bar');
SpreadsheetApp.getUi().alert(message);
function noBreakSpaces(txt) {
return txt.replace(/ /g, '\xa0');
}
}