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..
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.
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));
If I misunderstood your question, please tell me. I would like to modify it.
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.