将列表框的选定值传递到google-apps-script中的另一个列表框

时间:2013-08-31 19:45:25

标签: javascript google-app-engine google-apps-script

我正在构建一个谷歌应用程序用户界面,我无法从一个单独的列表框中的回调事件确定一个列表框中项目的价值。我试过传递隐藏的事件,但这没有成功。如何将所选成绩传递给_clickCourse

这是对英文代码的粗略描述:

1)使用所有可用的唯一成绩填充第一个列表框 2)附加要在等级列表框的内容改变时触发的功能 3)在2中创建的函数中,我通过even知道所选的成绩,使用该信息过滤正确的课程,用有效的课程填充课程列表框。 4)将回调函数附加到课程列表框 5)在此功能内,过滤所选成绩+选定课程以确定有效学期。

...问题是最后一个函数通过谷歌UI传递给它的事件知道所选择的课程,但它不知道所选的成绩。

Sample Data:

Grade    Semester     Course                 Unit
1          1        Chemistry101         Chemistry
3          2        Physics321            Physics
2          1        Chemistry101         Chemistry


 function executionStartsHere() { 
   var doc = SpreadsheetApp.getActiveSpreadsheet();
   var app = UiApp.createApplication().setTitle('Course Planner');
   var panel = app.createVerticalPanel();
   panel.setId("main-panel");


   //grades
   panel.add(_getLabel("Grades"));
   uniqueGrades = _findUniqueGrades();
   gradeListBox = _getListBox(app,uniqueGrades,"grade-listbox","grades");
   var gradeHandler = app.createServerHandler('_clickGrade');
   gradeListBox.addChangeHandler(gradeHandler);
   panel.add(gradeListBox);

   //courses
   panel.add(_getLabel("Courses"));
   courseListBox = _getListBox(app,{},"course-listbox","courses");
   var courseHandler = app.createServerHandler('_clickCourse');
   courseListBox.addChangeHandler(courseHandler);
   panel.add(courseListBox);

   //semester
   panel.add(_getLabel("Semester"));
   semesterListBox = _getListBox(app,{},"semester-listbox","semesters");
   panel.add(semesterListBox);


   h = app.createHidden("selectedGrade").setId("selectedGrade");
   h.setValue("1");
   panel.add(h);

   //finally
   app.add(panel);
   doc.show(app);  
 }


 function _clickGrade(eventInfo) {
   var app = UiApp.getActiveApplication();
   // get value of ListBox based on name
   var selectedGrade = eventInfo.parameter.grades;
   Logger.log("In clickGrade, selected:"+selectedGrade);
   //eventInfo.parameter.selectedGrade=selectedGrade;
   app.getElementById("selectedGrade").setValue(selectedGrade);
   Logger.log("eventInfo.parameter.selectedGrade: "+eventInfo.parameter.selectedGrade);
    _displayCourses(selectedGrade);
   return app;
 }

 function _clickCourse(eventInfo) {
   //WE NEED TO SOMEHOW PASS THE SELCTED GRADE IN HERE

   var app    = UiApp.getActiveApplication();
   var grade  = app.getElementById("selectedGrade")
   var grade2 = eventInfo.parameter.selectedGrade;
   var course = eventInfo.parameter.courses;
    Logger.log("Selected grade, grade2, course: "+grade+" ,"+grade2+" ,"+course);
   _displaySemesters(grade,course);
   return app;
 }

 function _displayCourses(grade) {
   var app = UiApp.getActiveApplication();
   validCourses = _findValidCourses(grade);
   lb = app.getElementById("course-listbox");
   _populateListBox(lb,validCourses);
 }

 function _displaySemesters(grade,course){
   var app = UiApp.getActiveApplication();
   validSemesters = _findValidSemesters(grade,course);
   lb = app.getElementById("semester-listbox");
   _populateListBox(lb,validSemesters);
 }


 function _findUniqueGrades(){
   var workBook = _getWorkBook(); 
   var sheet = _getLearningOutcomeSheet(workBook);
   var graderows = getColumnAsArray(sheet, 1); 

   var grades= {}; 
   for(var i=1; i<graderows.length; i++) { //skip the first row (labels)
     if(graderows[i] != "" && graderows[i] != undefined ) {
       grades[graderows[i]]=graderows[i];
     }
   }
   return grades;
 }

 function _findValidCourses(grade) {
  var workBook = _getWorkBook();
  var sheet = _getLearningOutcomeSheet(workBook);
  var graderows = getColumnAsArray(sheet, 1); 
  var courserows = getColumnAsArray(sheet, 3);

  // loop over courserows
  //  if the grade for this courserow matches the grade we are searching for
  //     then store this courserow in an associative array
  validCourses = {};
  for(var i=0; i< courserows.length; i++) {
    if ( graderows[i] == grade ) {
      validCourses[courserows[i]] = courserows[i];
    }
  }
  return validCourses;
 } 

 function _findValidSemesters(grade,course){
  var workBook     = _getWorkBook();
  var sheet        = _getLearningOutcomeSheet(workBook);
  var graderows    = getColumnAsArray(sheet, 1);
  var courserows   = getColumnAsArray(sheet, 3);
  var semesterrows = getColumnAsArray(sheet, 2); 

   validSemesters = {};
   for(var i=0; i< semesterrows.length; i++){
     if ( courserows[i] == course && graderows[i] == grade ){
       validSemesters[semesterrows[i]] = semesterrows[i];
     }
   }
   return validSemesters;
 }

 function _getListBox(app,items,id,label){
   var lb = app.createListBox().setId(id).setName(label);
   lb.setVisibleItemCount(1);
   //add items, if any
   for each ( i in items) {
     lb.addItem(i);
   }
   return lb;
 } 

 function _populateListBox(listBox,items){
   listBox.clear();
   Logger.log("ListBox ID: "+ listBox.getId());
   for each ( i in items) {
     Logger.log("adding item: " + i);
     listBox.addItem(i);
   }
 }

 function _getWorkBook(){
   return SpreadsheetApp.openById(employee_SPREADSHEET);
 }

 function _getLearningOutcomeSheet(workBook){
   return workBook.getSheets()[0];
 }

 function _getLabel(string){
   var app = UiApp.getActiveApplication();
   return app.createLabel(string); 
 }

 function getColumnAsArray(sheet,column) {
  var dataRange = sheet.getRange(1,column,99,1);
  var data = dataRange.getValues()
  //Logger.log("column values: " + data);

  return data;
 }

2 个答案:

答案 0 :(得分:1)

我认为您只是忘记为隐藏的窗口小部件设置名称,您只提供了一个ID ...而且您显然知道事件参数使用窗口小部件名称来获取其值。 :-)

h = app.createHidden("selectedGrade").setId("selectedGrade").setName("selectedGrade");

编辑:

Oooops,你说得对,我起初并没有注意到。

在你的代码中玩了一下后,我发现你在哪里用你的处理程序伪造一个callBackElement,我还添加了第二个隐藏的小部件来保存课程值。 这是你的代码的副本,我用textBoxes替换了隐藏的小部件,看看发生了什么。 它似乎按预期工作,你可以在一切正常时隐藏textBoxes或者再次用隐藏的小部件替换它们。

 function executionStartsHere() { 
   var doc = SpreadsheetApp.getActiveSpreadsheet();
   var app = UiApp.createApplication().setTitle('Course Planner');
   var panel = app.createVerticalPanel();
   panel.setId("main-panel");


   //grades
   panel.add(_getLabel("Grades"));
   uniqueGrades = _findUniqueGrades();
   gradeListBox = _getListBox(app,uniqueGrades,"grade-listbox","grades");
   var gradeHandler = app.createServerHandler('_clickGrade').addCallbackElement(panel);
   gradeListBox.addChangeHandler(gradeHandler);
   panel.add(gradeListBox);

   //courses
   panel.add(_getLabel("Courses"));
   courseListBox = _getListBox(app,{item:'First choose a grade value'},"course-listbox","courses");
   var courseHandler = app.createServerHandler('_clickCourse').addCallbackElement(panel);
   courseListBox.addChangeHandler(courseHandler);
   panel.add(courseListBox);

   //semester
   panel.add(_getLabel("Semester"));
   semesterListBox = _getListBox(app,{item:'First choose a course value'},"semester-listbox","semesters");
   panel.add(semesterListBox);


   var h = app.createTextBox().setName("selectedGrade").setId("selectedGrade");
   var h2 = app.createTextBox().setName("selectedCourse").setId("selectedCourse");
   panel.add(h).add(h2);

   //finally
   app.add(panel);
   doc.show(app);  
 }


 function _clickGrade(eventInfo) {
   var app = UiApp.getActiveApplication();
   // get value of ListBox based on name
   var selectedGrade = eventInfo.parameter.grades;
   Logger.log("In clickGrade, selected:"+selectedGrade);
   //eventInfo.parameter.selectedGrade=selectedGrade;
   app.getElementById("selectedGrade").setValue(selectedGrade);
   Logger.log("eventInfo.parameter.selectedGrade: "+eventInfo.parameter.selectedGrade);
    _displayCourses(selectedGrade);
   return app;
 }

 function _clickCourse(eventInfo) {
   //WE NEED TO SOMEHOW PASS THE SELCTED GRADE IN HERE

   var app    = UiApp.getActiveApplication();
   var grade  = app.getElementById("selectedGrade")
   var grade2 = eventInfo.parameter.selectedGrade;
   var course = eventInfo.parameter.courses;
   app.getElementById("selectedCourse").setValue(course);
    Logger.log("Selected grade, grade2, course: "+grade+" ,"+grade2+" ,"+course);
   _displaySemesters(grade2,course);
   return app;
 }

 function _displayCourses(grade) {
   var app = UiApp.getActiveApplication();
   validCourses = _findValidCourses(grade);
   lb = app.getElementById("course-listbox");
   _populateListBox(lb,validCourses);
 }

 function _displaySemesters(grade,course){
   var app = UiApp.getActiveApplication();
   Logger.log(grade+' '+course)
   validSemesters = _findValidSemesters(grade,course);
   lb = app.getElementById("semester-listbox");
   _populateListBox(lb,validSemesters);
 }


 function _findUniqueGrades(){
   var workBook = _getWorkBook(); 
   var sheet = _getLearningOutcomeSheet(workBook);
   var graderows = getColumnAsArray(sheet, 1); 

   var grades= {}; 
   for(var i=1; i<graderows.length; i++) { //skip the first row (labels)
     if(graderows[i] != "" && graderows[i] != undefined ) {
       grades[graderows[i]]=graderows[i];
     }
   }
   return grades;
 }

 function _findValidCourses(grade) {
  var workBook = _getWorkBook();
  var sheet = _getLearningOutcomeSheet(workBook);
  var graderows = getColumnAsArray(sheet, 1); 
  var courserows = getColumnAsArray(sheet, 3);

  // loop over courserows
  //  if the grade for this courserow matches the grade we are searching for
  //     then store this courserow in an associative array
  validCourses = {};
  for(var i=0; i< courserows.length; i++) {
    if ( graderows[i] == grade ) {
      validCourses[courserows[i]] = courserows[i];
    }
  }
  return validCourses;
 } 

 function _findValidSemesters(grade,course){
  var workBook     = _getWorkBook();
  var sheet        = _getLearningOutcomeSheet(workBook);
  var graderows    = getColumnAsArray(sheet, 1);
  var courserows   = getColumnAsArray(sheet, 3);
  var semesterrows = getColumnAsArray(sheet, 2); 

   validSemesters = {};
   for(var i=0; i< semesterrows.length; i++){
     if ( courserows[i] == course && graderows[i] == grade ){
       validSemesters[semesterrows[i]] = semesterrows[i];
     }
   }
   return validSemesters;
 }

 function _getListBox(app,items,id,label){
   var lb = app.createListBox().setId(id).setName(label);
   lb.setVisibleItemCount(1);
   //add items, if any
   for each ( i in items) {
     lb.addItem(i);
   }
   return lb;
 } 

 function _populateListBox(listBox,items){
   listBox.clear();
   Logger.log("ListBox ID: "+ listBox.getId());
   for each ( i in items) {
     Logger.log("adding item: " + i);
     listBox.addItem(i);
   }
 }

 function _getWorkBook(){
   return SpreadsheetApp.getActiveSpreadsheet()
 }

 function _getLearningOutcomeSheet(workBook){
   return workBook.getSheets()[0];
 }

 function _getLabel(string){
   var app = UiApp.getActiveApplication();
   return app.createLabel(string); 
 }

 function getColumnAsArray(sheet,column) {
  var dataRange = sheet.getRange(1,column,99,1);
  var data = dataRange.getValues()
  //Logger.log("column values: " + data);

  return data;
 }

答案 1 :(得分:0)

使用setTag()和getTag()传递这些循环中的值。然后将标记值传递给eventinfo参数中的处理程序。