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?
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()
countryDropDown
and vendorDropDown
can be written by one function.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.
getDropdownLists
using google.script.run
with var lists = ["country", "vendor"]
.getDropdownList
, which is your script, and ["country", "vendor"]
using a loop.getDropdownList
, the dropdown lists are created with createDropDown
.// 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);
}
});
}
Please add the following function. This function is called from Javascript side.
function getDropdownLists(e) {
return e.map(f => ({id: f, values: getDropdownList(f)}));
}
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.