Search code examples
loopsuser-interfacegoogle-apps-scriptgoogle-sheetsprompt

How to create a popup using Google Apps Script only if a certain button is pushed on the previous popup and remove text box?


My code below opens 4 text boxes, 1 asking a YES/NO question and 3 asking OK/CANCEL questions. Every one of them accepts text, but I want the first question to only accept buttons YES and NO without a text box. Also, if the answer to the first question is NO, I want it to skip the second question and go straight to the third question.

Questions are as follows:

  • Did we do work for this client today/yesterday? [YES/NO]
  • What did we help this client with today? [textbox][OK/CANCEL] ***skip if previous answer is no
  • When should we follow up with this client next? (MM/DD/YY) [textbox][OK/CANCEL]
  • Next Follow Up Activity? [textbox][OK/CANCEL]
function TextBox() {
  var ui = SpreadsheetApp.getUi();
  const sheet = SpreadsheetApp.openById('1JZ-v5n5m0_lyaoLgsHDAa78AtYNP5AsUDo2NRpJbwG4').getSheetByName('HOT & WARM CLIENTS');
  sheet.sort(9)

  var today = Utilities.formatDate(new Date(), "GMT-6", "MM/dd/yy")
  var ui = SpreadsheetApp.getUi();
  var valuesToCopy = sheet.getRange("C5:J5").getDisplayValues().map(r => r.join(' \n '));

var ar2 = [
{
  message: valuesToCopy + "\n\n Did we do work for this client today/yesterday? (YES/NO)",
  range: "G5"
}

];

var ar3 = [
    
    { // promptUPDATE3
      message: valuesToCopy + "\n\n What did we help this client with today?",
      range: "H5"
    },
    
  ];

var ar = [
    // { // promptUPDATE3
    //   message: valuesToCopy + "\n\n What did we help this client with today?",
    //   range: "PREVIOUS"
    // },
    { // promptUPDATE
      message: valuesToCopy + "\n\n When should we follow up with this client next? (MM/DD/YY)",
      range: "I5"
    },
    { // promptUPDATE2
      message: valuesToCopy + "\n\n Next Follow Up Activity?",
      range: "J5"
    }
  ];

ar2.forEach(({message, range }) => {
  var res = ui.prompt(message, ui.ButtonSet.YES_NO);
  if (res.getSelectedButton() == ui.Button.YES) {
    sheet.getRange("G5").setValue(today);

  } else { 
    Logger.log('The user clicked "No" or the dialog\'s close button.');
  }
})


  ar3.forEach(({ message, range }) => {
    var res = ui.prompt(message, ui.ButtonSet.OK_CANCEL);
    var lastRowSourceData = sheet.getRange("H5").getValue();
    var lastActivity = SpreadsheetApp.openById('1JZ-v5n5m0_lyaoLgsHDAa78AtYNP5AsUDo2NRpJbwG4').getSheetByName('HOT & WARM CLIENTS').getRange("H5").getValue();
    if (res.getSelectedButton() == ui.Button.OK) {
      sheet.getRange(range).setValue(lastActivity+" | "+res.getResponseText());
    } else {
      Logger.log('The user clicked "No" or the dialog\'s close button.');
    }
  })



  ar.forEach(({ message, range }) => {
    var res = ui.prompt(message, ui.ButtonSet.OK_CANCEL);
    if (res.getSelectedButton() == ui.Button.OK) {
      sheet.getRange(range).setValue(res.getResponseText());

    } else {
      Logger.log('The user clicked "No" or the dialog\'s close button.');
    }
  })
  
  }

Solution

  • SUGGESTION [SCRIPT UPDATED]

    Perhaps you can try this tweaked script below. This will use the alert Class Ui method for the first question & an if condition to skip the order of questions if the user selects NO.

    In my understanding, here is the flow you want to achieve:

    1. The first question should not have a Textbox but only give the user to select YES or NO buttons.
    2. If user selects NO on the first question, route the user to the third question instead.
    3. Otherwise, the user will be prompted with the questions in order.

    NOTE: This sample script will only process the first client on your sheet just like on your actual script. If I have misunderstood something Or if there's anything else missing, feel free to let me know.

    Sample Tweaked Script

    function TextBox() {
      var ui = SpreadsheetApp.getUi();
      var today = Utilities.formatDate(new Date(), "GMT-6", "MM/dd/yy");
      const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('HOT & WARM CLIENTS');
      sheet.sort(9);
      var sheetData = sheet.getRange("C5:J5").getDisplayValues();
      var questions = [['\n\n Did we do work for this client today/yesterday? (YES/NO)', "G"],
      ['\n\n What did we help this client with today?', "H"],
      ['\n\n When should we follow up with this client next? (MM/DD/YY)', "I"],
      ['\n\n Next Follow Up Activity?', "J"]];
    
    
      var row = "5"; //ONLY process current client on 5th row
      var clientDetails = sheetData[0].join('\n');
    
      /**First question */
      var q1 = ui.alert(clientDetails + questions[0][0], ui.ButtonSet.YES_NO)
      if (q1 == ui.Button.YES) {
        sheet.getRange(questions[0][1] + row).setValue(today);
        /**End of the First question */
    
        /**Second question */
        var q2 = ui.prompt(clientDetails + questions[1][0], ui.ButtonSet.OK_CANCEL);
        var lastActivity = sheet.getRange(questions[1][1] + row).getValue();
        q2.getSelectedButton() == ui.Button.OK ? sheet.getRange(questions[1][1] + row).setValue(lastActivity + " | " + q2.getResponseText()) : console.log('The user clicked "No" or the dialog\'s close button.');
        /**End of the Second question*/
    
        /**Third question */
        var q3 = ui.prompt(clientDetails + questions[2][0], ui.ButtonSet.OK_CANCEL);
        q3.getSelectedButton() == ui.Button.OK ? sheet.getRange(questions[2][1] + row).setValue(q3.getResponseText()) : console.log('The user clicked "No" or the dialog\'s close button.');
        /**End of the Third question*/
    
        /**Fourth question */
        var q4 = ui.prompt(clientDetails + questions[3][0], ui.ButtonSet.OK_CANCEL);
        q4.getSelectedButton() == ui.Button.OK ? sheet.getRange(questions[3][1] + row).setValue(q4.getResponseText()) : console.log('The user clicked "No" or the dialog\'s close button.');
        /**End of the Fourth question*/
      } else {
        /**Skip to the third question if 'NO' was selected on the first question*/
        var q3 = ui.prompt(clientDetails + questions[2][0], ui.ButtonSet.OK_CANCEL);
        q3.getSelectedButton() == ui.Button.OK ? sheet.getRange(questions[2][1] + row).setValue(q3.getResponseText()) : console.log('The user clicked "No" or the dialog\'s close button.');
    
        /**End to fourth question*/
        var q4 = ui.prompt(clientDetails + questions[3][0], ui.ButtonSet.OK_CANCEL);
        q4.getSelectedButton() == ui.Button.OK ? sheet.getRange(questions[3][1] + row).setValue(q4.getResponseText()) : console.log('The user clicked "No" or the dialog\'s close button.');
    
      }
    }
    

    Demonstration

    - Quick Test

    enter image description here

    - E.g. If the user selects NO on the first question & then gets skipped to the third question

    enter image description here

    References