【发布时间】:2021-11-11 10:23:43
【问题描述】:
在此post 之后,我想在用户选择下拉选项时更改 HTML 下拉列表中的数据。 在这里,我想根据用户选择的字段过滤数据。不同的是,过滤后是在一个 GS 文件中。
编辑:简而言之,我想和之前的帖子一样,但有两个参数
这是我的代码。
谷歌脚本
// uniqueContractScopeData, uniqueAcTypeData and uniqueCustomerData are tables of Strings.
They send the expected data.
function get_joCustomer() {
var jo = {};
var dataArray = [];
for (var i=0; i < uniqueCustomerData.length; i++ ) {
var record = {};
record['customer'] = uniqueCustomerData[i];
dataArray.push(record);
}
jo.user = dataArray;
return JSON.stringify(jo);
}
function get_joContractScope() {
var jo = {};
var dataArray = [];
for( var i=0; i < uniqueContractScopeData.length; i++ ) {
var record = {};
record['contractScope'] = uniqueContractScopeData[i];
dataArray.push(record);
}
jo.user = dataArray;
return JSON.stringify(jo);
}
function get_joAcType() {
var jo = {};
var dataArray = [];
for( var i=0; i < uniqueAcTypeData.length; i++ ) {
var record = {};
record['acType'] = uniqueAcTypeData[i];
dataArray.push(record);
}
jo.user = dataArray;
return JSON.stringify(jo);
}
// Work
function processGet_jo(element, returnedAssociatedData) {
var uniqueData = getUniqueAssociatedElement(element, returnedAssociatedData); // Returns expected result which is a table of Strings
var jo = {};
var dataArray = [];
var result;
for (var i=0; i < uniqueData.length; i++ ) {
var record = {};
record[returnedAssociatedData] = uniqueData[i];
dataArray.push(record);
}
jo.user = dataArray;
return JSON.stringify(jo);
}
.html
<html>
<head>
<base target="_top">
</head>
<body>
<!-- form -->
<form id="filterForm" onChange="displayAppropriatedValues(this)" onSubmit="handleFormSubmitFilter(this)">
<label for="customer"> Customer </label><br/>
<select id="customer" name="customer">
<option selected></option><br/>
</select><br/><br/>
<label for="contractScope"> Contract Scope </label><br/>
<select name="contractScope" id="contractScope">
<option selected></option><br/>
</select><br/><br/>
<label for="acType"> AC Type </label><br/>
<select multiple id="acType" name="acType">
<option></option><br/>
</select><br/><br/>
<button type="submit"> Submit </button>
</form>
<script>
//When the form is opened, dropdown options are generated. No problems here
var customerHtml = document.getElementById("customer");
var customerHtmlValue = customerHtml.options[customerHtml.selectedIndex].value;
var contractScopeHtml = document.getElementById("contractScope");
var contractScopeHtmlValue = contractScopeHtml.options[contractScopeHtml.selectedIndex].value;
var acTypeHtml = document.getElementById("acType");
var acTypeHtmlValue = contractScopeHtml.options[contractScopeHtml.selectedIndex].value;
(function () {
google.script.run.withSuccessHandler(
function(value) {
var jo = JSON.parse(value);
for(var i=0; i < jo.user.length-1; i++) {
var user = jo.user[i];
var option = document.createElement("option");
option.text = user.customer;
option.value = option.text;
customerHtml.add(option);
}
}
).get_joCustomer();
google.script.run.withSuccessHandler(
function(value) {
var jo = JSON.parse(value);
for( var i=0; i < jo.user.length-1; i++ ) {
var user = jo.user[i];
var option = document.createElement("option");
option.text = user.contractScope;
option.value = option.text;
contractScopeHtml.add(option);
}
}
).get_joContractScope();
google.script.run.withSuccessHandler(
function(value) {
var jo = JSON.parse(value);
for( var i=0; i < jo.user.length-1; i++ ) {
var user = jo.user[i];
var option = document.createElement("option");
option.text = user.acType;
option.value = option.text;
acTypeHtml.add(option);
}
}
).get_joAcType();
}());
function displayAppropriatedValues(form) {
if (customer.value == "A") {
// Delete all options
var L = contractScopeHtml.options.length;
for(var i = L; i >= 1; i--) {
contractScopeHtml.remove(i);
} // Work
//The problem is here. The customerHtmlValue and returnedAssociatedData parameters are not recognised at the end of the function.
(function () {
google.script.run.withSuccessHandler(
function(value) {
var jo = JSON.parse(value);
for( var i=0; i < jo.user.length-1; i++ ) {
var user = jo.user[i];
var option = document.createElement("option");
option.text = user.returnedAssociatedData;
option.value = option.text;
contractScopeHtml.add(option);
}
}
).processGet_jo(customerHtmlValue, "contractScope");
}());
}
</script>
如果您需要更多信息,请告诉我。
提前致谢
Ziganotschka 解决方案后:
gs
function processGet_jo(element, returnedAssociatedData) {
console.log("element: " + element);
console.log("returnedAssociatedData: " + returnedAssociatedData);
var uniqueData = [];
uniqueData.push(getUniqueAssociatedElement(element, returnedAssociatedData));
var jo = {};
var dataArray = [];
for (var i=0; i < uniqueData.length; i++ ) {
var record = {};
record[returnedAssociatedData] = uniqueData[i];
dataArray.push(record);
}
jo.user = dataArray;
return JSON.stringify(jo);
}
function doGet(element){
return HtmlService.createTemplateFromFile("FormFilter").evaluate();
}
html
<html>
<head>
<base target="_top">
</head>
<body>
<!-- form -->
<form id="filterForm" onSubmit="handleFormSubmitFilter(this)">
<label for="customer"> Customer </label><br/>
<select id="customer" name="customer" onChange="getcustomerHtmlValue()">
<option selected></option><br/>
</select><br/><br/>
<label for="contractScope"> Contract Scope </label><br/>
<select name="contractScope" id="contractScope">
<option selected></option><br/>
</select><br/><br/>
<label for="acType"> AC Type </label><br/>
<select multiple id="acType" name="acType">
<option></option><br/>
</select><br/><br/>
</form>
</body>
</html>
<script>
var customerHtml = document.getElementById("customer");
var customerHtmlValue = customerHtml.options[customerHtml.selectedIndex].value;
var contractScopeHtml = document.getElementById("contractScope");
var contractScopeHtmlValue = contractScopeHtml.options[contractScopeHtml.selectedIndex].value;
var acTypeHtml = document.getElementById("acType");
var acTypeHtmlValue = contractScopeHtml.options[contractScopeHtml.selectedIndex].value;
function getcustomerHtmlValue() {
google.script.run.withSuccessHandler(
function(value) {
console.log("value: " + value);
var jo = JSON.parse(value);
for (var i=0; i < jo.user.length-1; i++) {
var user = jo.user[i];
var option = document.createElement("option");
option.text = user.contractScope;
option.value = option.text;
contractScopeHtml.add(option);
}
}
).processGet_jo(customerHtmlValue, "contractScope");
}
</script>
【问题讨论】:
-
究竟是什么不适合您?如果没有完整的代码和/或示例数据,很难重现。
-
问题出在最后一个 .html 脚本函数中。用 ".processGet_jo(customerHtmlValue, "contractScope"); 调用的两个参数不被识别(之前的函数正常工作)。没有错误消息但没有任何反应
-
你的意思是那些参数没有从html正确传递到code.gs?您是否尝试记录它们?
-
我检查了警报,参数对除此之外的所有功能都很好。如果我理解的话,ProcessGet_jo() 会带来在 google.script.run.withSuccessHandler() 中处理的数据。但是数据不来是因为参数不识别
-
据我所知,变量
returnedAssociatedData未在代码中的任何位置定义。
标签: javascript json forms google-apps-script