首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Google脚本中ui.alert窗口中的拆分行和粗体文本

Google脚本中ui.alert窗口中的拆分行和粗体文本
EN

Stack Overflow用户
提问于 2020-04-09 01:34:05
回答 1查看 2.1K关注 0票数 5

我觉得这应该很简单,但我什么也找不到。我希望在ui.alert窗口中弹出的消息可以用粗体显示特定的单词,并将,处的字符串拆分为新的行。下面是我的代码:

代码语言:javascript
运行
复制
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);
}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-09 02:33:48

  • 您希望对类Ui中的alert(prompt, buttons)方法使用HTML标记。
  • 您希望将'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不能用于Ui类中的alert(prompt, buttons)方法。因此,作为一种解决办法,如何在类Ui中使用带有showModalDialog方法的自定义对话框?

当您的脚本被修改时,如下所示。

修改脚本:

请复制并粘贴以下脚本并运行send函数。这样,就打开了一个对话框。单击ok按钮和cancel按钮时,分别运行clickOk()clickCancel()

代码语言:javascript
运行
复制
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;
}

注意:

  • 这个修改过的脚本是一个简单的脚本。因此,请根据您的实际情况修改它。
  • 请使用启用V8运行脚本。

参考文献:

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61112601

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档