Split Lines and Bold Text within a ui.alert Window in Google Apps Script

Viewed 1909

I feel like this should be pretty simple, but I can't find anything about it. I want my message that pops up in a ui.alert window to bold certain words and split a string at , into new lines. Here is the code I have:

function send(){
  var ui = SpreadsheetApp.getUi();
  var bccSend = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('main_gen').getRange(2,2).getValue(); //value example is xxx@gmail.com, yyy@gmail.com
  var bccSendReplace = bccSend.toString().replace(/,/g,"<br>");
  var response = ui.alert('You are about to send this to the following email address(es): \n\n' + bccSendReplace + '\n\n Click OK to send, otherwise, close this box or click Cancel to abort.', ui.ButtonSet.OK_CANCEL);
}

The bccSendReplace is what I want to parse at commas into new lines. Instead, the code just replaces the comma with <br>. I would also like all text within bccSendReplace to be bold. Any ideas? Thanks!

1 Answers
  • You want to use HTML tags for the method of alert(prompt, buttons) in Class Ui.
  • You want to set 'You are about to send this to the following email address(es): \n\n' + bccSendReplace + '\n\n Click OK to send, otherwise, close this box or click Cancel to abort.' to the bold type.

Modification point:

  • Unfortunately, in the current stage, the HTML cannot be used for the method of alert(prompt, buttons) in Class Ui. So as a workaround, how about using the custom dialog with the method of showModalDialog in Class Ui?

When your script is modified, it becomes as follows.

Modified script:

Please copy and paste the following script and run the function of send. By this, a dialog is opened. When the ok button and the cancel button are clicked, clickOk() and clickCancel() are run, respectively.

function send(){
  var ui = SpreadsheetApp.getUi();
  var bccSend = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('main_gen').getRange(2,2).getValue();
  var bccSendReplace = bccSend.toString().replace(/,/g,"<br>");
  
  const str = 'You are about to send this to the following email address(es): \n\n' + bccSendReplace + '\n\n Click OK to send, otherwise, close this box or click Cancel to abort.';
  const html = `
    <b>${str}</b><br>
    <input type="button" value="ok" onClick="google.script.run.withSuccessHandler(() => google.script.host.close()).clickOk()">
    <input type="button" value="cancel" onClick="google.script.run.withSuccessHandler(() => google.script.host.close()).clickCancel()">
  `;
  ui.showModalDialog(HtmlService.createHtmlOutput(html), 'sample');
}

function clickOk() {
  // do something;
}

function clickCancel() {
  // do something;
}

Note:

  • This modified script is a simple script. So please modify it for your actual situation.
  • Please run the script with enabling V8.

References:

Related