Search code examples
javascriptgoogle-apps-scriptweb-applications

Passing 2 parameters to .withSuccessHandler()


I've the below code, that read the data in 2 sheets, country and vendor and build droupdown list for each of them.

I wanted to try comining them in a single function using something like:

var lists = ["country", "vendor"];
lists.forEach()
// or
// for list in lists {}

But stuck with the .withSuccessHandler()how can I pass 2 inputs to it, like list and list values?

<script>
  function createCountryDropdown() {
      //SUBMIT YOUR DATA RANGE FOR DROPDOWN AS THE PARAMETER
      google.script.run
        .withFailureHandler(onFailure)
        .withSuccessHandler(countryDropDown)
        .getDropdownList("country");

      google.script.run
        .withFailureHandler(onFailure)
        .withSuccessHandler(vendorDropDown)
        .getDropdownList("vendor");
  }
  function onFailure(err) {
      alert('There was an error!' + err.message);
  }
  //POPULATE COUNTRY DROPDOWNS
  function countryDropDown(values) { //Ref: https://stackoverflow.com/a/53771955/2391195
    var list = document.getElementById('country');   
    for (var i = 0; i < values.length; i++) {
      var option = document.createElement("option");
     // console.log(value[i])
      option.value = values[i];
      option.text = values[i];
      list.appendChild(option);
    }
  }

    //POPULATE Vendor DROPDOWNS
  function vendorDropDown(values) { //Ref: https://stackoverflow.com/a/53771955/2391195
    var list = document.getElementById('vendor');   
    for (var i = 0; i < values.length; i++) {
      var option = document.createElement("option");
     // console.log(value[i])
      option.value = values[i];
      option.text = values[i];
      list.appendChild(option);
    }
  }
</script>

Any help?


Solution

  • I believe your goal as follows.

    You want to combine 2 google.script.run in a single function as follows.

    var lists = ["country", "vendor"];
    lists.forEach()
    

    Modification points:

    • At first, I think that your countryDropDown and vendorDropDown can be written by one function.
    • I think that the values can be retrieved by one call in Google Apps Script side using var lists = ["country", "vendor"];. This has already been mentioned by Marios's answer.

    When above points are reflected to your script, it becomes as follows. And, the flow of this modified script is as follows.

    1. Call getDropdownLists using google.script.run with var lists = ["country", "vendor"].
    2. At Google Apps Script side, the values are retrieved from getDropdownList, which is your script, and ["country", "vendor"] using a loop.
    3. Using the retrieved values from getDropdownList, the dropdown lists are created with createDropDown.

    Modified script:

    JavaScript side:

    // I modified this function.
    function createCountryDropdown() {
      var lists = ["country", "vendor"];
      google.script.run
        .withFailureHandler(onFailure)
        .withSuccessHandler(createDropDown)
        .getDropdownLists(lists);
    }
    
    function onFailure(err) {
        alert('There was an error!' + err.message);
    }
    
    // I prepared this function.
    function createDropDown(obj) { //Ref: https://stackoverflow.com/a/53771955/2391195
      obj.forEach(({id, values}) => {
        var list = document.getElementById(id);
        for (var i = 0; i < values.length; i++) {
          var option = document.createElement("option");
          option.value = values[i];
          option.text = values[i];
          list.appendChild(option);
        }
      });
    }
    

    Google Apps Script side:

    Please add the following function. This function is called from Javascript side.

    function getDropdownLists(e) {
      return e.map(f => ({id: f, values: getDropdownList(f)}));
    }
    

    Note:

    Of course, you can use google.script.run in a loop. But I'm worry that in this case, the process cost will be higher than that of above direction.