Home > other >  What is the data type that is saved from a dialog box?
What is the data type that is saved from a dialog box?

Time:10-28

As the title says, I'm wondering what data type is returned when a user presses enter on a dialog box, specifically from a self-created dialog box in a google spreadsheet, as I was under the impression it would be an array. More specifically, I have a dialog box that appears that allows text user input. This input is then supposed to be saved from the dialog box to cells in a google spreadsheet. Currently it only displays undefined Vessels for each of the cells. Below is the relevant code:

function setVesselCounts(data){
  let keys = scs.getRange(6,1,12,1).getDisplayValues()
  for(var i = 0; i < keys.length; i  ){
    scs.getRange(6,3,6 i).setValue(data[i]   " Vessels")
  }
}

Below is the html used to create the dialog box, as well as the JS inside the HTML

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <style>
      p {display: table-row; }
      label { display: table-cell; }
      input { display: table-cell; }
    </style>
    <script>
      function setVesselCounts(){
        let controls = {}
        document.querySelectorAll("input[type=text]").forEach(input => controls[input.id] = input.value)
        validateInput(controls) ? google.script.run.withSuccessHandler(google.script.host.close).setVesselCounts(controls) : window.alert('Please Check Input')
      }

      function validateInput(controls){
        return (
          controls["LA/LB"].value != '' &&
          controls["Oakland"].value != '' &&
          controls["Tacoma"].value != '' &&
          controls["Seattle"].value != '' &&
          controls["Vancouver"].value != ''&&
          controls["Halifax"].value != '' &&
          controls["NY"].value != '' &&
          controls["Norfolk"].value != '' &&
          controls["Charleston"].value != '' &&
          controls["Savannah"].value != '' &&
          controls["Mobile"].value != '' &&
          controls["Houston"].value != ''
        )
      }
    </script>
  </head>
  
  
  <body>
    <div>
      <table >
        <p>
          <label for="LA/LB">LA/LB: </label><input type="text" id="LA/LB" />
        </p>
        <p>
          <label for="Oakland">Oakland: </label><input type="text" id="Oakland">
        </p>
        <p>
          <label for="Tacoma">Tacoma: </label><input type="text" id="Tacoma">
        </p>
        <p>
          <label for="Seattle">Seattle: </label><input type="text" id="Seattle">
        </p>
        <p>
          <label for="Vancouver">Vancouver: </label><input type="text" id="Vancouver">
        </p>
        <p>
          <label for="Halifax">Halifax: </label><input type="text" id="Halifax">
        </p>
        <p>
          <label for="New York">New York: </label><input type="text" id="NY">
        </p>
        <p>
          <label for="Norfolk">Norfolk: </label><input type="text" id="Norfolk">
        </p>
        <p>
          <label for="Charleston">Charleston: </label><input type="text" id="Charleston">
        </p>
        <p>
          <label for="Savannah">Savannah: </label><input type="text" id="Savannah">
        </p>
        <p>
          <label for="Mobile">Mobile: </label><input type="text" id="Mobile">
        </p>
        <p>
          <label for="Houston">Houston: </label><input type="text" id="Houston">
        </p>
      </table>
      <input type="button" value="Submit"  onclick="setVesselCounts()" >
      <input type="button" value="Close" onclick="google.script.host.close()" />
    </div>
  </body>
</html>

Finally is a screenshot of the created dialog boxenter image description here

CodePudding user response:

Since your values are contiguous, capture the values in an array and use only one setValues() instead of repeated setValue(). Note that I am constructing a 2D array in rows.

Change

function setVesselCounts(data){
  let keys = scs.getRange(6,1,12,1).getDisplayValues()
  for(var i = 0; i < keys.length; i  ){
    scs.getRange(6,3,6 i).setValue(data[i]   " Vessels")
  }
}

To

function setVesselCounts(data){
  let keys = scs.getRange(6,1,12,1).getDisplayValues();
  let rows = [];
  for(var i = 0; i < keys.length; i  ){
    rows.push([data[keys[i][0]]   " Vessels"]);
  }
  scs.getRange(6,3,12,1).setValues(rows);
}

Reference

  • Related