Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Apps Script - conditional user input - number prompt

I need to get user input which should be a positive whole number (Integer). I figured out how to keep displaying prompt if the input is not a number, but I cant figure out how to write condition for number value. Here is the code.

var ui = SpreadsheetApp.getUi(); // Same variations.

  do {
  var result = ui.prompt(
      'Please fill in',
      'Please enter number of people involved',
      ui.ButtonSet.OK_CANCEL);
  var text = parseInt(result.getResponseText());
  var button = result.getSelectedButton();
  }
  while (!(button == ui.Button.CANCEL) && isNaN(parseInt(text)) && !(button == ui.Button.CLOSE));

  var button = result.getSelectedButton();
  if (button == ui.Button.OK) {
    ui.alert('Number of people wanted: ' + text + '.');
  } else if (button == ui.Button.CANCEL) {
    ui.alert('Action cancelled.');
    return;
  } else if (button == ui.Button.CLOSE) {
    ui.alert('Action cancelled.');
    return;
  }

I tried, in my opinion, all the possible combinations of <,>,!, &&, ||, but nothing worked..

like image 249
romanzdk Avatar asked May 13 '26 17:05

romanzdk


2 Answers

  • You want to exit the while loop only when the value is the positive integer.

If my understanding is correct, how about this modification? I think that there are several answers for your situation. So please think of this as just one of them.

Modified script:

From:
while (!(button == ui.Button.CANCEL) && isNaN(parseInt(text)) && !(button == ui.Button.CLOSE));
To:
while (!(button == ui.Button.CANCEL) && !(/^[0-9]+$/.test(text)) && !(button == ui.Button.CLOSE));

Reference:

  • test()

If I misunderstood your question, please tell me. I would like to modify it.

Edit:

In your current modified script, for example, if the values like 123-456 and 123abc are inputted, 123 is retrieved. This is due to var text = parseInt(result.getResponseText()).

If you don't want to do this, please modify var text = parseInt(result.getResponseText()) to var text = result.getResponseText(). By this, when such values are inputted, it doesn't exit the while loop.

like image 186
Tanaike Avatar answered May 17 '26 14:05

Tanaike


You will have to check for the value of the text in the loop itself. The reason being if you have NaN value in a variable text and try to check if its value in less than zero i.e. text < 0. It will always return false, which cause the while to loop to break and cause the remaining code to execute.

You will have to check for the value of the variable text in a particular sequence: 1) Check if it a number or not 2) If it a number, check if it is less than zero 3) If less than zero, check if the prompt has been canceled or closed? 4) If neither then change the value of text to NaN, which will cause the loop to continue execution. Like so:

if (!isNaN(text)){
      if( text < 0 ){
        if(!(button == ui.Button.CANCEL) && !(button == ui.Button.CLOSE))
         text = NaN
      }
    }

This will be your final code:

function checkNum(){
  var ui = SpreadsheetApp.getUi(); // Same variations.

  do {
  var result = ui.prompt(
      'Please fill in',
      'Please enter number of people involved',
      ui.ButtonSet.OK_CANCEL);
  var text = parseInt(result.getResponseText());
  var button = result.getSelectedButton();
    if (!isNaN(text)){
      if( text < 0 ){
        if(!(button == ui.Button.CANCEL) && !(button == ui.Button.CLOSE))
         text = NaN
      }

    }
  }
  while ((!(button == ui.Button.CANCEL) && !(button == ui.Button.CLOSE) && isNaN(text)));

  //var button = result.getSelectedButton();
  if (button == ui.Button.OK) {
    ui.alert('Number of people wanted: ' + text + '.');
  } else if (button == ui.Button.CANCEL) {
    ui.alert('Action cancelled.');
    return;
  } else if (button == ui.Button.CLOSE) {
    ui.alert('Action cancelled.');
    return;
  }
}
like image 33
Jack Brown Avatar answered May 17 '26 15:05

Jack Brown