列表框选择到文本框

时间:2013-09-28 22:33:20

标签: listbox google-apps-script textfield

我有一个ListBox,它填充了一个充满教师姓名和电话分机的电子表格。我希望能够选择一个教师的名字,并让它自动填充教师的电话分机。我发现有几种方法可以通过C ++实现,但没有一种方法可以使用Google Apps脚本...

          //Create a panel which holds all the form elelemnts
          var vrtMainPanel = app.createVerticalPanel().setId('vrtMainPanel');

          //Create Spreadsheet Source
          var spSheet = SpreadsheetApp.openById('0Aur3owCpuUY-dFF0dVZXb3I1Yjlpbzg3SXFIaklEcUE');
          var spTeacherList = spSheet.getSheetByName('TeacherList');
          var spSubjectList = spSheet.getSheetByName('SubjectList');
          var spPeriodList = spSheet.getSheetByName('PeriodList');
          var spCountList = spSheet.getSheetByName('CountList');

          //Create the form elements
          var hdlExt = app.createServerHandler('getExt').addCallbackElement(vrtMainPanel);
          var hdlTeacherName = app.createServerHandler('getTeacherName').addCallbackElement(vrtMainPanel);
          var lbxTeacherName = app.createListBox().setId('lbxTeacherName').setName('lbxTeacherName').addChangeHandler(hdlExt).addChangeHandler(hdlTeacherName);
          var lstTeacherNames = spTeacherList.getRange(1,1,spTeacherList.getLastRow(),1).getValues();
              lstTeacherNames.sort();

              for (var l = 0; l < lstTeacherNames.length; l++) {
                lbxTeacherName.addItem(lstTeacherNames[l],l);
              }

          var lblTeacherName = app.createLabel('Teacher Name:');
          var txtTeacherName = app.createTextBox().setName('txtTeacherName').setId('txtTeacherName').setVisible(false);

          var lblExt = app.createLabel('Ext:');
          var txtExt = app.createTextBox().setName('txtExt').setId('txtExt');

       //Set DateBox to Tomorrow's Date
          var tomorrow =new Date(new Date(new Date().setHours(0,0,0,0)).setDate(new Date().getDate() + 1));// set hours, min, sec & milliSec to 0 and day=day+1
          Logger.log(tomorrow);
          var lblDate = app.createLabel('Date of Test:');
          var boxDate = app.createDateBox().setId('boxDate').setName('boxDate').setFormat(UiApp.DateTimeFormat.DATE_SHORT).setValue(tomorrow);

          var lbxSubject = app.createListBox().setId('lbxSubject').setName('lbxSubject');
          var lstSubjects = spSubjectList.getRange(1,1,spSubjectList.getLastRow(),1).getValues();
              lstSubjects.sort();

              for (var l = 0; l < lstSubjects.length; l++) {
                lbxSubject.addItem(lstSubjects[l]);
              }

          var lbxPeriod = app.createListBox().setId('lbxPeriod').setName('lbxPeriod');
          var lstPeriods = spPeriodList.getRange(1,1,spPeriodList.getLastRow(),1).getValues();
              lstPeriods.sort();

              for (var l = 0; l < lstPeriods.length; l++) {
                lbxPeriod.addItem(lstPeriods[l]);
              }

          var lblStudentNum = app.createLabel('Number of Students:');
          var lbxStudentNum = app.createListBox().setId('lbxStudentNum').setName('lbxStudentNum');
          var lstStudentNums = spCountList.getRange(1,1,spCountList.getLastRow(),1).getValues();
              lstStudentNums.sort();

              for (var l = 0; l < lstStudentNums.length; l++) {
                lbxStudentNum.addItem(lstStudentNums[l]);
              }

          var txtSourceGrp = app.createTextBox().setName('txtSourceGrp').setVisible(false);
          var txtTypeGrp = app.createTextBox().setName('txtTypeGrp').setVisible(false);
          var txtElementsID = app.createTextBox().setName('txtElementsID').setText('Elements Test ID').setVisible(false);
          var txtQuiaLink = app.createTextBox().setName('txtQuiaLink').setText('Quia Test Link').setVisible(false);
          var txtQuiaPass = app.createTextBox().setName('txtQuiaPass').setText('Quia Test Passphrase').setVisible(false);

          //Create Source Radio Button Group
          var radHCopy = app.createRadioButton('group1', 'Hard-Copy').setFormValue('Hard-Copy').addClickHandler(app.createClientHandler().forTargets(txtSourceGrp).setText('Hard-Copy'));
          var radECopy = app.createRadioButton('group1', 'Electronic-Copy').setFormValue('Electronic-Copy').addClickHandler(app.createClientHandler().forTargets(txtSourceGrp).setText('Electronic-Copy'));

          //Create Type Radio Button Group
          var radTExam = app.createRadioButton('group2', 'Teacher-Made Exam').setFormValue('Teacher-Made Exam').addClickHandler(app.createClientHandler().forTargets(txtTypeGrp).setText('Teacher-Made Exam'));
          var radEExam = app.createRadioButton('group2', 'Elements Exam').setFormValue('Elements Exam').addClickHandler(app.createClientHandler().forTargets(txtTypeGrp).setText('Elements Exam'));
          var radQExam = app.createRadioButton('group2', 'Quia Exam').setFormValue('Quia Exam').addClickHandler(app.createClientHandler().forTargets(txtTypeGrp).setText('Quia Exam'));

          var btnCreate = app.createButton('Create Event');

          //Client Handlers for textBoxes
          var showTxtElementHandler = app.createClientHandler().forTargets(txtElementsID).setVisible(true);
          var hideTxtElementHandler = app.createClientHandler().forTargets(txtElementsID).setVisible(false);
              radEExam.addClickHandler(showTxtElementHandler);
              radTExam.addClickHandler(hideTxtElementHandler);
              radQExam.addClickHandler(hideTxtElementHandler);


          var showTxtQuiaLinkHandler = app.createClientHandler().forTargets(txtQuiaLink).setVisible(true);
          var hideTxtQuiaLinkHandler = app.createClientHandler().forTargets(txtQuiaLink).setVisible(false);
              radQExam.addClickHandler(showTxtQuiaLinkHandler);
              radTExam.addClickHandler(hideTxtQuiaLinkHandler);
              radEExam.addClickHandler(hideTxtQuiaLinkHandler);

          var showTxtQuiaPassHandler = app.createClientHandler().forTargets(txtQuiaPass).setVisible(true);
          var hideTxtQuiaPassHandler = app.createClientHandler().forTargets(txtQuiaPass).setVisible(false);
              radQExam.addClickHandler(showTxtQuiaPassHandler);
              radTExam.addClickHandler(hideTxtQuiaPassHandler);
              radEExam.addClickHandler(hideTxtQuiaPassHandler);

          //Create handler which will execute 'createEvents(e)' on clicking the button
          var evtHandler = app.createServerClickHandler('createEvents');
              evtHandler.addCallbackElement(vrtMainPanel);

          //Add this handler to the button
              btnCreate.addClickHandler(evtHandler);

          //Add all the elemnts to the panel 
          var formGrid = app.createGrid(12,3).setCellPadding(3);
          vrtMainPanel.add(formGrid);
          formGrid
          .setWidget(0,0,lbxTeacherName)
          .setWidget(0,1,txtExt)
          .setWidget(0,2,txtTeacherName)
          .setWidget(1,0,lbxPeriod)
          .setWidget(1,1,lbxSubject)
          .setWidget(2,0,lblDate)
          .setWidget(2,1,boxDate)
          .setWidget(3,0,lblStudentNum)
          .setWidget(3,1,lbxStudentNum)
          .setWidget(4,0,radHCopy)
          .setWidget(4,1,radECopy)
          .setWidget(5,0,radTExam)
          .setWidget(6,0,radEExam)
          .setWidget(6,1,txtElementsID)
          .setWidget(7,0,radQExam)
          .setWidget(7,1,txtQuiaLink)
          .setWidget(8,1,txtQuiaPass)
          .setWidget(9,0,txtSourceGrp)
          .setWidget(9,1,txtTypeGrp)
          .setWidget(10,0,btnCreate)

          //Add this panel to the application
          app.add(vrtMainPanel);

          //Return the application
          return app;
        }

        function getExt(e){
          var spSheet = SpreadsheetApp.openById('0Aur3owCpuUY-dFF0dVZXb3I1Yjlpbzg3SXFIaklEcUE');
          var spTeacherList = spSheet.getSheetByName('TeacherList');
          var lstTeacherNames = spTeacherList.getRange(1,2,spTeacherList.getLastRow(),1).getValues();
          var app = UiApp.getActiveApplication();
          var txtExt = app.getElementById('txtExt');
              txtExt.setText(lstTeacherNames[Number(e.parameter.lbxTeacherName)][0]);// we get the value in the 2D array returned by getValues()
          return app;
        }

        function getTeacherName(e){
          var spSheet = SpreadsheetApp.openById('0Aur3owCpuUY-dFF0dVZXb3I1Yjlpbzg3SXFIaklEcUE');
          var spTeacherList = spSheet.getSheetByName('TeacherList');
          var lstTeacherNames = spTeacherList.getRange(1,1,spTeacherList.getLastRow(),1).getValues();
          var app = UiApp.getActiveApplication();
          var txtTeacherName = app.getElementById('txtTeacherName');
              txtTeacherName.setText(lstTeacherNames[e.parameter.lbxTeacherName][0]);// we get the value in the 2D array returned by getValues()
          return app;
        }

// CREATE EVENT FUNCTION

function createEvents(e){
  //Get the active application
  var app = UiApp.getActiveApplication();

  try{
    //Get the entries
    var ssTeacher = e.parameter.txtTeacherName;
    var ssExt = e.parameter.txtExt;
    var ssSubject = e.parameter.lbxSubject;
    var ssPeriod = e.parameter.lbxPeriod;
    var ssStudentNum = e.parameter.lbxStudentNum;
    var ssSource = e.parameter.txtSourceGrp;
    var ssType = e.parameter.txtTypeGrp;
    var ssElementsID = e.parameter.txtElementsID;
    var ssQuiaLink = e.parameter.txtQuiaLink;
    var ssQuiaPass = e.parameter.txtQuiaPass;
    var eventDate = e.parameter.boxDate;
    var eventCalSubject = ssPeriod + ": " + ssTeacher + " (" + ssStudentNum + ")";
    var eventCalDetails = "Extension: " + ssExt + "\n" +
          "Subject: " + ssSubject + "\n\n" +
          "Source: " + ssSource + "\n" +
          "Type: " + ssType + "\n" +
          "Elements ID: " + ssElementsID + "\n" +
          "Quia Test Link: " + ssQuiaLink + "\n" +
          "Quia Passphrase: " + ssQuiaPass;

    //Get the calendar
    var cal = CalendarApp.getCalendarById('davie.k12.nc.us_d2mv2eb8aspuant1vb5j6r3sis@group.calendar.google.com');//Change the calendar id
    //Create the events
    var newID = cal.createAllDayEvent(eventCalSubject, eventDate, {description:eventCalDetails}).getId();

    //Log the entries in a spreadsheet
    var sheet = SpreadsheetApp.openById('0Aur3owCpuUY-dGJIOGZ1LXhqT2FNMGVXSGNJazFnUmc').getActiveSheet();//Change the spreadhseet key to yours
    var lastRow = sheet.getLastRow();
    var targetRange = sheet.getRange(lastRow+1, 1, 1, 13).setValues([[new Date(),eventDate,ssTeacher,ssExt,ssSubject,ssPeriod,ssSource,ssType,ssElementsID,ssQuiaLink,ssQuiaPass,ssStudentNum,newID]]);
    return app;

    //Show the confirmation message
    app.add(app.createLabel('Kurzweil Calendar Event created successfully...'));
    //Make the form panel invisible
    app.getElementById('vertMainPanel').setVisible(false);
    return app;
  }

  //If an error occurs, show it on the panel
  catch(e){
    app.add(app.createLabel('Error occured: '+ e));
    return app;
  }
}

1 个答案:

答案 0 :(得分:0)

正如我在第一篇评论中提到的那样,你必须创建一个服务器处理程序来获取它,但你的脚本需要一些小修改:

  1. 必须修改listBox以返回数字索引而不是名称,这将简化在电子表格中获取正确数据的过程(当addItem(arg1,arg2)有2个参数时,第一个显示第一个,而第二个是返回到处理函数&gt;&gt;你必须修改你的其他处理函数,使用我在getPhone(e)函数中使用的相同原理)
  2. 您创建的标签不可见,因此我更改了网格值
  3. 您从电子表格中获取此新版本数据的方式确实是一个好主意,我很遗憾我自己没有考虑过它; - )
  4. 请注意,e.parameter.varName返回的值是字符串,这就是我使用Number(e.parameter.lbxTeacherName)获取电子表格数据值的原因。
  5. 我认为教师数据表B栏中的电话分机...如果没有在getPhone处理程序功能中相应修改。
  6. (编辑:我改变了“明天”定义以跳过字符串操作,但它是一个细节)

    以下是完整修改后的代码:

        function doGet() {
          var app = UiApp.createApplication().setTitle('DHS: Kurzweil Calendar');
    
          //Create a panel which holds all the form elelemnts
          var vrtMainPanel = app.createVerticalPanel().setId('vrtMainPanel');
    
          //Create Spreadsheet Source
          var spSheet = SpreadsheetApp.openById('0Aur3owCpuUY-dFF0dVZXb3I1Yjlpbzg3SXFIaklEcUE');
          var spTeacherList = spSheet.getSheetByName('TeacherList');
          var spSubjectList = spSheet.getSheetByName('SubjectList');
          var spPeriodList = spSheet.getSheetByName('PeriodList');
          var spCountList = spSheet.getSheetByName('CountList');
    
          //Create the form elements
          var lblTeacherName = app.createLabel('Teacher Name:');
          var teacherNameHandler = app.createServerHandler('getPhone').addCallbackElement(vrtMainPanel);
          var lbxTeacherName = app.createListBox().setId('lbxTeacherName').setName('lbxTeacherName').addChangeHandler(teacherNameHandler);
          var lstTeacherNames = spTeacherList.getRange(1,1,spTeacherList.getLastRow(),1).getValues();
              lstTeacherNames.sort();
    
              for (var l = 0; l < lstTeacherNames.length; l++) {
                lbxTeacherName.addItem(lstTeacherNames[l],l);
              }
    
          var lblExt = app.createLabel('Ext:');
          var txtExt = app.createTextBox().setName('txtExt').setId('txtExt');
    
       //Set DateBox to Tomorrow's Date
          var tomorrow =new Date(new Date(new Date().setHours(0,0,0,0)).setDate(new Date().getDate() + 1));// set hours, min, sec & milliSec to 0 and day=day+1
          Logger.log(tomorrow);
          var lblDate = app.createLabel('Date:');
          var boxDate = app.createDateBox().setId('boxDate').setName('boxDate').setFormat(UiApp.DateTimeFormat.DATE_SHORT).setValue(tomorrow);
    
          var lbxSubject = app.createListBox().setId('lbxSubject').setName('lbxSubject');
          var lstSubjects = spSubjectList.getRange(1,1,spSubjectList.getLastRow(),1).getValues();
              lstSubjects.sort();
    
              for (var l = 0; l < lstSubjects.length; l++) {
                lbxSubject.addItem(lstSubjects[l]);
              }
    
          var lbxPeriod = app.createListBox().setId('lbxPeriod').setName('lbxPeriod');
          var lstPeriods = spPeriodList.getRange(1,1,spPeriodList.getLastRow(),1).getValues();
              lstPeriods.sort();
    
              for (var l = 0; l < lstPeriods.length; l++) {
                lbxPeriod.addItem(lstPeriods[l]);
              }
    
          var lblStudentNum = app.createLabel('Number of Students:');
          var lbxStudentNum = app.createListBox().setId('lbxStudentNum').setName('lbxStudentNum');
          var lstStudentNums = spCountList.getRange(1,1,spCountList.getLastRow(),1).getValues();
              lstStudentNums.sort();
    
              for (var l = 0; l < lstStudentNums.length; l++) {
                lbxStudentNum.addItem(lstStudentNums[l]);
              }
    
          var txtSourceGrp = app.createTextBox().setName('txtSourceGrp').setVisible(false);
          var txtTypeGrp = app.createTextBox().setName('txtTypeGrp').setVisible(false);
          var txtElementsID = app.createTextBox().setName('txtElementsID').setText('Elements Test ID').setVisible(false);
          var txtQuiaLink = app.createTextBox().setName('txtQuiaLink').setText('Quia Test Link').setVisible(false);
          var txtQuiaPass = app.createTextBox().setName('txtQuiaPass').setText('Quia Test Passphrase').setVisible(false);
    
          //Create Source Radio Button Group
          var radHCopy = app.createRadioButton('group1', 'Hard-Copy').setFormValue('Hard-Copy').addClickHandler(app.createClientHandler().forTargets(txtSourceGrp).setText('Hard-Copy'));
          var radECopy = app.createRadioButton('group1', 'Electronic-Copy').setFormValue('Electronic-Copy').addClickHandler(app.createClientHandler().forTargets(txtSourceGrp).setText('Electronic-Copy'));
    
          //Create Type Radio Button Group
          var radTExam = app.createRadioButton('group2', 'Teacher-Made Exam').setFormValue('Teacher-Made Exam').addClickHandler(app.createClientHandler().forTargets(txtTypeGrp).setText('Teacher-Made Exam'));
          var radEExam = app.createRadioButton('group2', 'Elements Exam').setFormValue('Elements Exam').addClickHandler(app.createClientHandler().forTargets(txtTypeGrp).setText('Elements Exam'));
          var radQExam = app.createRadioButton('group2', 'Quia Exam').setFormValue('Quia Exam').addClickHandler(app.createClientHandler().forTargets(txtTypeGrp).setText('Quia Exam'));
    
          var btnCreate = app.createButton('Create Event');
    
          //Client Handlers for textBoxes
          var showTxtElementHandler = app.createClientHandler().forTargets(txtElementsID).setVisible(true);
          var hideTxtElementHandler = app.createClientHandler().forTargets(txtElementsID).setVisible(false);
              radEExam.addClickHandler(showTxtElementHandler);
              radTExam.addClickHandler(hideTxtElementHandler);
              radQExam.addClickHandler(hideTxtElementHandler);
    
    
          var showTxtQuiaLinkHandler = app.createClientHandler().forTargets(txtQuiaLink).setVisible(true);
          var hideTxtQuiaLinkHandler = app.createClientHandler().forTargets(txtQuiaLink).setVisible(false);
              radQExam.addClickHandler(showTxtQuiaLinkHandler);
              radTExam.addClickHandler(hideTxtQuiaLinkHandler);
              radEExam.addClickHandler(hideTxtQuiaLinkHandler);
    
          var showTxtQuiaPassHandler = app.createClientHandler().forTargets(txtQuiaPass).setVisible(true);
          var hideTxtQuiaPassHandler = app.createClientHandler().forTargets(txtQuiaPass).setVisible(false);
              radQExam.addClickHandler(showTxtQuiaPassHandler);
              radTExam.addClickHandler(hideTxtQuiaPassHandler);
              radEExam.addClickHandler(hideTxtQuiaPassHandler);
    
          //Create handler which will execute 'createEvents(e)' on clicking the button
          var evtHandler = app.createServerClickHandler('createEvents');
              evtHandler.addCallbackElement(vrtMainPanel);
    
          //Add this handler to the button
              btnCreate.addClickHandler(evtHandler);
    
          //Add all the elemnts to the panel 
          var formGrid = app.createGrid(12,2).setCellPadding(3);
          vrtMainPanel.add(formGrid);
          formGrid
          .setWidget(0,0,lblTeacherName)
          .setWidget(1,0,lbxTeacherName)
          .setWidget(0,1,lblExt)
          .setWidget(1,1,txtExt)
          .setWidget(2,0,lbxPeriod)
          .setWidget(2,1,lbxSubject)
          .setWidget(3,0,lblDate)
          .setWidget(3,1,boxDate)
          .setWidget(4,0,lblStudentNum)
          .setWidget(4,1,lbxStudentNum)
          .setWidget(5,0,radHCopy)
          .setWidget(5,1,radECopy)
          .setWidget(6,0,radTExam)
          .setWidget(7,0,radEExam)
          .setWidget(7,1,txtElementsID)
          .setWidget(8,0,radQExam)
          .setWidget(8,1,txtQuiaLink)
          .setWidget(9,1,txtQuiaPass)
          .setWidget(10,0,txtSourceGrp)
          .setWidget(10,1,txtTypeGrp)
          .setWidget(11,0,btnCreate)
    
          //Add this panel to the application
          app.add(vrtMainPanel);
    
          //Return the application
          return app;
        }
    
        function getPhone(e){
          var spSheet = SpreadsheetApp.openById('0Aur3owCpuUY-dFF0dVZXb3I1Yjlpbzg3SXFIaklEcUE');
          var spTeacherList = spSheet.getSheetByName('TeacherList');
          var lstTeacherNames = spTeacherList.getRange(1,2,spTeacherList.getLastRow(),1).getValues();
          var app = UiApp.getActiveApplication();
          var txtExt = app.getElementById('txtExt');
          txtExt.setText(lstTeacherNames[Number(e.parameter.lbxTeacherName)][0]);// we get the value in the 2D array returned by getValues() 
          return app;
        }
    

    编辑:关注您的上次评论。

    虽然您用来检索教师姓名的方法非常好(我想我确实选择了相同的方法)但还有另一种方法可以实现它,这种方法不太明显但在“数字方面更有效”代码行“。

    将它作为TAG存储在分机电话号码上并从createEvent()处理程序中检索它的技巧。 代码只需要一个非常小的修改(从SS获取2列数据并为标记赋值+获取该值 - 请参阅代码中的注释)所以我不会重现整个事情,只是下面的相关部分:

    function getPhone(e){
      var spSheet = SpreadsheetApp.openById('0AnqSFd3iikE3dEtBQndOYVNEbFVWcDlyQmFoaUV3a1E');
      var spTeacherList = spSheet.getSheetByName('TeacherList');
      var lstTeacherNames = spTeacherList.getRange(1,1,spTeacherList.getLastRow(),2).getValues();// get 2 columns instead of only one
      Logger.log(lstTeacherNames);
      var app = UiApp.getActiveApplication();
      var txtExt = app.getElementById('txtExt');
      txtExt.setText(lstTeacherNames[Number(e.parameter.lbxTeacherName)][1]);// set the phone number 
      txtExt.setTag(lstTeacherNames[Number(e.parameter.lbxTeacherName)][0]);// set teacher's name in the TAG
      return app;
    }
    
    
    function createEvents(e){
      //Get the active application
      var app = UiApp.getActiveApplication();
        //Get the entries
        var ssTeacher = e.parameter.txtExt_tag;
      Logger.log('teacher = '+ssTeacher)
      ...