SpreadsheetApp.getUi().alert('foo bar')

When I use this piece of code, it appears as 'foo bar', removing the additional spaces in between. is there a way coerce Google App Script to do otherwise? It appears to only remove additional spaces, which makes sense, but I'd rather have spaces than dashes. If there isn't a way to do it through the UI API, what alternatives do I have?

  • Just realized the code formatting also removed the extraneous spaces in the question. I'll try to edit it to reflect what I meant.
    – David Tan
    Mar 20, 2022 at 12:02

1 Answer 1


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');
  • Oh thanks this works! That's a nifty trick I'll keep in mind.
    – David Tan
    Mar 21, 2022 at 3:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.