Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 [email protected], [email protected]
  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!

like image 665
hunter21188 Avatar asked Apr 09 '20 01:04

hunter21188


People also ask

How do I split a string in Appscript?

var array1 = [{}]; var string1 = "A, B, C, D"; array1 = string1. split(","); The problem is based on this kind of coding for example in flash. The string1 will split all "," then transfers it to the array1 in this format ["A","B","C", "D"] .

How do I make text bold in Google Sheets?

Select the text you want to modify. To bold text, click the Bold text button or press Ctrl+B (Windows) or Command+B (Mac) on your keyboard. The text will change to bold.


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:

  • alert(prompt, buttons)
  • showModalDialog(userInterface, title)
like image 114
Tanaike Avatar answered Sep 19 '22 23:09

Tanaike