Skip to main content
added version with function
Source Link
Blindspots
  • 7.5k
  • 5
  • 20
  • 38

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');
  }
}

Instead of using spaces consider to use non-breaking spaces \xa0. The following code uses String.prototype.replace and a regular expression to replace all the spaces by non-breaking spaces.

function myFunction() {
  const message = 'foo        bar'.replace(/ /g,'\xa0');
  SpreadsheetApp.getUi().alert(message);
}

Instead of using spaces consider using non-breaking spaces \xa0 instead. 

The following code uses the String.prototype.replace with a regular expression / /g * to replace all spaces with 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');
  }
}
Source Link

Instead of using spaces consider to use non-breaking spaces \xa0. The following code uses String.prototype.replace and a regular expression to replace all the spaces by non-breaking spaces.

function myFunction() {
  const message = 'foo        bar'.replace(/ /g,'\xa0');
  SpreadsheetApp.getUi().alert(message);
}