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

大兔子大兔子 提交于 2020-05-15 05:29:22

问题


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:


  • 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.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

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)

If I misunderstood your question and this was not the direction you want, I apologize.



来源:https://stackoverflow.com/questions/61112601/split-lines-and-bold-text-within-a-ui-alert-window-in-google-apps-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!