Home > Mobile >  How to add conditionals to user input in App Scripts with while loops?
How to add conditionals to user input in App Scripts with while loops?

Time:03-20

I made a selectBox which had its range of values from a Google Sheet Column. I also want to take an Integer input value from the user and then write this value in a specific cell according to option taken from selectBox. The html link does not show the integer response box. Is it possible to do the above plan in a while loop? Would appreciate any ideas and correction of code


function doGet() {


  var ap = SpreadsheetApp.openByUrl("Gsheet URL here");

  var ui = SpreadsheetApp.getUi();
  var user = ui.prompt("Put down a number");
  var result = result.getSelectedButton();

  var sheet = ap.getSheetByName("lv");
  var values = sheet.getRange("A2:A10").getValues();
  var options = values.map(function(row)
{
  #To show show the selected option??
  var  item  = options.getSelecteditem();

  
  if (item === A3)
      {
      var cell = SpreadsheetApp.getActiveSheet().getActiveCell();
       var a1 = cell.getA3Notation();
       var val = cell.getValue();
       SpreadsheetApp.getUi().alert("Ur value is  " a1 " value is " val);
      }
      
  
   {
    return '<option value="'   row[0]   '">'   row[0]   '</option>';
  });
  var html = '<form onSubmit="handleSubmit(this)">  Type of Cuisine'   options.join('')   '</select></form>';
  return HtmlService.createHtmlOutput(html);
}


CodePudding user response:

Using an Html Dialog to Control User Inputs

Not sure what you wanted so here's a complete example I whipped up for you.

Code.gs:

function processInput(obj) {
  Logger.log(JSON.stringify(obj));
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('Sheet0');
  const [min,max,locs] = sh.getRange('B1:B3').getValues().flat();
  Logger.log('min: %s max: %s locs: %s',min,max,locs)
  const lA = locs.split(',');

  if(obj.int > max) {
    obj.msg = "Too High Try Again";
    return obj;
  } else if (obj.int < min) {
    obj.msg = "To Low Try Again";
    return obj;
  } else if (!~lA.indexOf(obj.loc)) {
    obj.msg = "Invalid Location";
    return obj;
  } else {
    sh.getRange(obj.loc).setValue(obj.int);
    obj.msg = "Complete";
    return obj;
  }
}

Following function Launches the dialog:

function launchInputDialog() {
  SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutputFromFile('ah1'),"Enter Input");
}

html:

<!DOCTYPE html>
<html>
<head>
</head>
<style>input {margin: 2px 5px 2px 0;}</style>
<body>
  <form>
    <input type="text" id="in1" placeholder="Enter an integer" />
    <br /><input type="text" id="in2" placeholder="Enter a location" />
    <br /><input type="button" value="Process" onClick="processinput();" />
  </form>
  <div id="msg"></div>
  <script>
    function processinput() {
      document.getElementById("msg").innerHTML = '';
      let v1 = parseInt(document.getElementById('in1').value);
      let v2 = document.getElementById('in2').value;
      let obj = {int:v1,loc:v2,msg:''};
      google.script.run
      .withSuccessHandler(robj => {
        console.log(JSON.stringify(robj))
        if(robj.msg == "Complete") {
          document.getElementById("msg").innerHTML = `Value: ${robj.int} Location: ${robj.loc} Try Again`;
          document.getElementById("in1").value = '';
          document.getElementById("in2").value = '';
        } else {
          document.getElementById("msg").innerHTML = robj.msg;
        }
      })
      .processInput(obj);


    }
  </script>
 
</body>

</html>

Short Demo:

enter image description here

  • Related