0
votes

I'm currently trying to create a custom UI dialogs with google app scripts.

Here's my current script:

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
    }
  }
}
}

Now, I have found that browser message boxes cannot support custom buttons but a UI can.

I'm trying to add this to the code for each if statement, however each one will be different.

Code:

// 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');
 }

Essentially, I want a custom dialog to appear when a particular value is inputted as seen in the IF statements. In that dialog box, it will show course information as well as the ability to close the box by selecting, "Ok" and also they can view the guide by clicking, "Guide".

Any help is appreciated.

1
this looks identical to another question from yesterday - Zig Mandel

1 Answers

0
votes

This is extremely similar to the question you asked yesterday, which you should have responded to instead of making a new question. That being said, what I provided clearly wasn't helpful enough - you can't do ui.ButtonSet.MadeUpButton. You need to use HTML to serve custom content, with a button:

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")
}

Try running that code - when you click the button, logMe() is run. You can append whatever HTML you want to the htmlOutput variable inside of your if statements. You can have two buttons which either call separate functions, or pass a variable to one function. For example, google.script.run.logMe(selectedOption). Hopefully that help resolves the issue you're having...