Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I add line breaks to the prompt in a google apps script dialogue box?

I’m attempting to provide a multi-line prompt message in a dialogue box using g-app script.

The resulting dialogue box would be:


Content of line 1.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Content of line 2.

Content of line 3.

Are you sure you want to continue?

YES / NO


Here’s the code:

function testing() {

var ui = DocumentApp.getUi();

const line1 = 'Content of line 1.';
const line2 = 'Content of line 2.';
const line3 = 'Content of line 3';

var dialogueMessage = [line1]+'\n'+
                [line2]+'\n'+
                [line3]

Logger.log(dialogueMessage); //Test message content. Looks correct, with three lines.

var result = ui.alert(
      dialogueMessage,
      'Are you sure you want to continue?',
      ui.ButtonSet.YES_NO);

}

The three separate lines appear when I log the message, but the dialogue box itself does not reflect any line breaks. Anyone know how I can add those breaks into the dialogue message?

Thx

>Solution :

Try:

function myFunction() {

  const ui = DocumentApp.getUi();

  const line1 = 'Content of line 1.';
  const line2 = 'Content of line 2.';
  const line3 = 'Content of line 3';

  const dialogueMessage = `${line1}\n${line2}\n${line3}`

  const result = ui.alert(
        `${dialogueMessage}\n\nAre you sure you want to continue?`,
        ui.ButtonSet.YES_NO);

}

Your error was that you were not including your dialogueMessage correctly.

var result = ui.alert(
  dialogueMessage,
  'Are you sure you want to continue?',
  ui.ButtonSet.YES_NO);

Corrected:

var result = ui.alert(
  '' + dialogueMessage + '\nAre you sure you want to continue?',
  ui.ButtonSet.YES_NO);

See also:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading