我目前正在尝试使用Google应用脚本创建自定义UI对话框。
这是我目前的剧本:
function onEdit(e) {
var cell = e.range;
var cellColumn = cell.getColumn();
var cellSheet = cell.getSheet().getName();
var cellValue = e.value;
var sheet = "System_Info";
if (cellSheet !== sheet) {
if (cellColumn === 4 || cellColumn === 5) {
if (cellValue === "PT1 - Induction Training") {
Browser.msgBox('Course Information', "PT1 - Induction Training:\\n\\nType: Theory \\n\\nTime Needed: 15-20 Minutes\\n\\n Learning Resource/Manual: BBACT, Forum Post: Rules for Officers" , Browser.Buttons.YES_NO); //Add the course name and the message that I want to popup. Course name should be exactly the same as in the list (case sensitive)
}
else if (cellValue === "PT2 - Traffic & Communications - Part 1") {
Browser.msgBox("PT2 - Traffic & Communications - Part 1 - Starting a Patrol/Loadouts: \\n\\nType: Practical \\n\\nTime Needed: 15-20 Minutes\\n\\n Learning Resource/Manual: PT2 Guide \\n Section: 1, 2"); //// add as many "else if" conditions as you want"
} else if (cellValue === "PT2 - Traffic & Communications - Part 2") {
}
else if (cellValue === undefined) { }
else {
Browser.msgBox('You have entered an invalid data, please select correct data corresponding to the Column.'); //This is in case they put a wrong course name
}
}
}
}
现在,我发现浏览器消息框不支持自定义按钮,但UI可以。
我正在尝试将其添加到每个if语句的代码中,但每个语句都会有所不同。
代码:
// Display a dialog box with a message and "Ok" and "Guide" buttons. The user can also close the
// dialog by clicking the close button in its title bar.
var ui = SpreadsheetApp.getUi();
var response = ui.alert("PT1 - Induction Training:\\n\\nType: Theory \\n\\nTime Needed: 15-20 Minutes\\n\\n Learning Resource/Manual: BBACT, Forum Post: Rules for Officers", ui.ButtonSet.OK_GUIDE);
// Process the user's response.
if (response == ui.Button.OK) {
Logger.log('The user clicked "Ok."');
} else {
Logger.log('The user clicked "Guide", open up guide link: www.britishborders.co.uk/laws');
}
基本上,我希望在输入特定值时出现自定义对话框,如IF语句中所示。在该对话框中,它将显示课程信息以及通过选择“确定”关闭该框的功能,并且他们还可以通过单击“指南”查看该指南。
感谢任何帮助。
答案 0 :(得分:0)
这与您昨天提出的问题非常相似,您应该回答这个问题而不是提出新问题。话虽这么说,我提供的内容显然不够有用 - 你不能做ui.ButtonSet.MadeUpButton。您需要使用HTML来提供自定义内容,只需按一下按钮:
function buttonDemo(){
var htmlOutput = HtmlService
.createHtmlOutput('<p>A change of speed, a change of style...</p><br><button onclick="google.script.run.logMe()">Close</button>')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setWidth(250)
.setHeight(300);
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'My add-on');
}
function logMe(){
Logger.log("You've been logged")
}
尝试运行该代码 - 单击该按钮时,将运行logMe()。您可以将所需的任何HTML附加到if语句中的htmlOutput变量中。您可以使用两个按钮来调用单独的函数,或将变量传递给一个函数。例如,google.script.run.logMe(selectedOption)。希望有助于解决您遇到的问题......