Google Apps 脚本中 ui.alert 窗口中的分割线和粗体文本

hun*_*188 5 google-sheets google-apps-script

我觉得这应该很简单,但我找不到任何相关内容。我希望我的消息在 ui.alert 窗口中弹出,以粗体显示某些单词并将字符串拆分,为新行。这是我的代码:

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);
}
Run Code Online (Sandbox Code Playgroud)

bccSendReplace就是我想用逗号解析成新行的内容。相反,代码只是将逗号替换为<br>。我还希望其中的所有文本bccSendReplace都是粗体。有任何想法吗?谢谢!

Tan*_*ike 5

  • 您想对 Ui 类中的方法使用 HTML 标签alert(prompt, buttons)
  • 您想设置'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.'为粗体。

修改点:

  • 不幸的是,在现阶段,HTML不能用于alert(prompt, buttons)Ui类中的方法。因此,作为一种解决方法,如何通过 Ui 类中的方法使用自定义对话框showModalDialog

当你的脚本被修改后,它会变成如下所示。

修改后的脚本:

请复制并粘贴以下脚本并运行 的功能send。这样,就会打开一个对话框。当单击“确定”按钮和“取消”按钮时,clickOk()分别clickCancel()运行。

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;
}
Run Code Online (Sandbox Code Playgroud)

笔记:

  • 这个修改后的脚本是一个简单的脚本。所以请根据您的实际情况进行修改。
  • 请在启用 V8 的情况下运行脚本。

参考: