Javascript 将2个参数传递给.withSuccessHandler()
我有下面的代码,它读取了两张表中的数据,Javascript 将2个参数传递给.withSuccessHandler(),javascript,google-apps-script,Javascript,Google Apps Script,我有下面的代码,它读取了两张表中的数据,国家和供应商,并为每一张表建立了详细列表 我想尝试使用以下方法将它们合并到单个函数中: var lists = ["country", "vendor"]; lists.forEach() // or // for list in lists {} 但是仍然使用.withSuccessHandler()如何向它传递两个输入,例如列表和列表值 <script> function createCount
国家
和供应商
,并为每一张表建立了详细列表
我想尝试使用以下方法将它们合并到单个函数中:
var lists = ["country", "vendor"];
lists.forEach()
// or
// for list in lists {}
但是仍然使用.withSuccessHandler()
如何向它传递两个输入,例如列表
和列表值
<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>
函数createCountryDropdown(){
//将数据范围作为参数提交下拉列表
google.script.run
.withFailureHandler(onFailure)
.withSuccessHandler(国家/地区下拉列表)
.getDropdownList(“国家”);
google.script.run
.withFailureHandler(onFailure)
.使用SuccessHandler(供应商下拉列表)
.getDropdownList(“供应商”);
}
功能失效(err){
警报(“出现错误!”+错误消息);
}
//填充国家/地区下拉列表
函数countryDropDown(值){//Ref:https://stackoverflow.com/a/53771955/2391195
var list=document.getElementById('country');
对于(变量i=0;i
有什么帮助吗?说明:
可以将形式为{country,vendor}
的对象作为函数参数
为了缩短代码,我使用了,但如果您愿意,可以随意使用您自己的版本
类似地,服务器端函数getDropdownList
需要返回一个与中的函数使用的相同形式的对象
return { country: country_values, vendor: vendor_values };
解决方案:
将客户端功能更改为:
function createCountryDropdown() {
google.script.run
.withFailureHandler(onFailure)
.withSuccessHandler(({ country, vendor }) => {
var listCountry = document.getElementById('country');
var listVendor = document.getElementById('vendor');
for (var i = 0; i < country.length; i++) {
var option = document.createElement("option");
option.value = country[i];
option.text = country[i];
listCountry.appendChild(option);
}
for (var j = 0; j < vendor.length; j++) {
var option = document.createElement("option");
option.value = vendor[j];
option.text = vendor[j];
listVendor.appendChild(option);
}
})
.getDropdownList();
}
function getDropdownList() {
// some code to get country_values
// some code to get vendor_values
return { country: country_values, vendor: vendor_values };
}
您现在可以删除函数countryDropDown
和vendorDropDown
,因为它们不再被与SuccessHandler一起使用
var lists = ["country", "vendor"];
lists.forEach()
您希望在单个函数中组合2google.script.run
,如下所示
var lists = ["country", "vendor"];
lists.forEach()
修改点:
- 首先,我认为您的
countryDropDown
和vendorDropDown
可以通过一个函数编写
- 我认为这些值可以通过使用
var list=[“country”,“vendor”]在googleapps脚本端调用一次来检索代码>。这一点我已经提过了
当上述各点反映到脚本中时,它将变成如下所示。并且,此修改脚本的流程如下所示
var lists = ["country", "vendor"];
lists.forEach()
使用google.script调用getDropdownLists
。使用var list=[“国家”、“供应商”]
运行
在Google Apps脚本端,使用循环从脚本getDropdownList
和[“国家”、“供应商”]
中检索值
使用从getDropdownList
检索到的值,使用createDropDown
创建下拉列表
修改脚本:
JavaScript方面:
注:
当然,您可以在循环中使用google.script.run
。但我担心在这种情况下,过程成本会高于上述方向。也许你想要这种方法?