Kenton Sparks
Kenton Sparks

Reputation: 145

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.

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

Upvotes: 0

Views: 1796

Answers (1)

NEWAZA
NEWAZA

Reputation: 1630

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:

Upvotes: 1

Related Questions