Search code examples
google-apps-scriptpromptcancel-button

Cancel Button UI Prompt


So I am having a little problem with my a prompt dialog I am making. The way this code works is whenever someone hits cancel on the ui prompt, it cancels the first prompt, but then the second prompt appears. Is there a way to make it so that when someone hits the cancel button is cancels both prompts? I am not sure if that makes any sense, but here is my code.

function Cancel() {
  var ui = SpreadsheetApp.getUi();

  var result = ui.prompt(
      'What Day Was Safe Rides Cancelled?',
      'Please Enter the Date as mm/dd/yyy.',
      ui.ButtonSet.OK_CANCEL);

  var result2 = ui.prompt(
      'What is the Reason for the Cancellation?',
      ui.ButtonSet.OK_CANCEL);

  // Process the user's response.
  var button = result.getSelectedButton();
  var text = result.getResponseText();
  var text2 = result2.getResponseText();
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Nightly Stats'),
    row = sheet.getLastRow();

  if (button == ui.Button.OK) {
    // User clicked "OK".
    sheet.insertRowAfter(row);
      sheet.getRange(row+1,1).setValue(text).setHorizontalAlignment("center");
      sheet.getRange(row+1,2,1,6).setValue(text2).setHorizontalAlignment("center").mergeAcross();
      sheet.getRange(row+1,8).setValue("1").setHorizontalAlignment("center");
    ui.alert('The Cancellation has Been Recorded');
  } else if (button == ui.Button.CANCEL) {
    // User clicked "Cancel".
    ui.alert('You closed the dialog.');
  } else if (button == ui.Button.CLOSE) {
    // User clicked X in the title bar.
    ui.alert('You closed the dialog.');
  }
}

Any help you can provide would be greatly appreciated. Thanks!


Solution

  • You basically need to move the "result2" prompt logic into the if (button == ui.Button.OK) {...} section.

    Also, I would recommend refactoring the "insertData" stuff into it's own function, so the button/prompt logic is easier to follow.

    Something like this:

    var SS = SpreadsheetApp.getActiveSpreadsheet();
    
    function Cancel() {
      var ui = SpreadsheetApp.getUi();
    
      // first prompt
      var result = ui.prompt(
        "What Day Was Safe Rides Cancelled?",
        "Please Enter the Date as mm/dd/yyy.",
        ui.ButtonSet.OK_CANCEL
      );
      var button = result.getSelectedButton();
      var text = result.getResponseText();
    
      // User clicked "OK" on first prompt
      if (button == ui.Button.OK) {
        // second prompt
        var result2 = ui.prompt(
          "What is the Reason for the Cancellation?",
          ui.ButtonSet.OK_CANCEL
        );
        var button2 = result2.getSelectedButton();
        var text2 = result2.getResponseText();
    
        // user clicked "OK" on second prompt
        if (button2 == ui.Button.OK) {
          insertData(text, text2);
        }
      }
    }
    
    function insertData(text, text2) {
      // get last row in nightly stats sheet
      var sheet = SS.getSheetByName("Nightly Stats");
      row = sheet.getLastRow();
    
      // insert data into sheet
      sheet.insertRowAfter(row);
      sheet
        .getRange(row + 1, 1)
        .setValue(text)
        .setHorizontalAlignment("center");
      sheet
        .getRange(row + 1, 2, 1, 6)
        .setValue(text2)
        .setHorizontalAlignment("center")
        .mergeAcross();
      sheet
        .getRange(row + 1, 8)
        .setValue("1")
        .setHorizontalAlignment("center");
    }