【发布时间】:2020-09-01 10:36:58
【问题描述】:
我正在 Google Scripts 中创建一个网络应用程序,以将信息插入到我的电子表格数据库中。但是,如果该项目已经在我的电子表格中,它将返回一些信息,让我知道它已经被添加。我已经能够让它工作,将两列信息返回到我的 web 应用程序。我的问题是,它的代码似乎太多了。我想看看是否有办法让它变得紧凑。我尝试使用数组、过滤器和 for 循环,但无法正常工作。 我正在学习javascript,因为我这样做。
对此的任何见解将不胜感激。
function getSSinfo(studentid){
var ss = SpreadsheetApp.openByUrl(url);
var ws = ss.getSheetByName("SSDatabse");
var data = ws.getRange(1, 1, ws.getLastRow(),10).getValues();
var ssNameList = data.map(function(r){ return r[0];});
var ssGroupList = data.map(function(r){ return r[7];});
var position = ssNameList.indexOf(studentid);
if(position > -1){
return ssGroupList[position];
} else {
return 'no found';
}
}
function getSSinfo1(studentid){
var ss = SpreadsheetApp.openByUrl(url);
var ws = ss.getSheetByName("SSDatabse");
var data = ws.getRange(1, 1, ws.getLastRow(),10).getValues();
var ssNameList = data.map(function(r){ return r[0];});
var ssGroupList = data.map(function(r){ return r[9];});
var position = ssNameList.indexOf(studentid);
if(position > -1){
return ssGroupList[position];
} else {
return 'no found';
}
}
function getInfo(){
var studentname = document.getElementById("sid").value.trim();
if (studentname.length >= 2){
var gscript = {}
gscript.usi = google.script.run.withSuccessHandler(updateSSinfo).getSSinfo(studentname);
gscript.usi1 = google.script.run.withSuccessHandler(updateSSinfo1).getSSinfo1(studentname);
google.script.run.withSuccessHandler(updateSSinfo1).getSSinfo1(studentname);
google.script.run.withSuccessHandler(updateSSinfo).getSSinfo(studentname);
}
}
function updateSSinfo(ssinfo){
document.getElementById("emailInfo").value = ssinfo;
}
function updateSSinfo1(sinfo){
document.getElementById("test").value = sinfo;
}
【问题讨论】:
标签: javascript html arrays google-sheets web-applications