根据RadioButton的状态显示/隐藏TextBox

时间:2013-09-26 15:47:12

标签: textbox google-apps-script radio-button

我想在点击radSource4按钮时显示“txtElementID”,在单击radSource5按钮时显示“txtQuiaLink”和“txtQuiaPass”。我该如何做...我对Google Apps脚本有点新,并尝试学习一些基础知识。感谢

    //Create the GUI form

function doGet() {
  var app = UiApp.createApplication().setTitle('DHS: Kurzweil Calendar');

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

  //Create the form elelemnts
  var lblTeacherName = app.createLabel('Teacher Name:');
  var txtTeacherName = app.createTextBox().setName('txtTeacherName');

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

  var lblDate = app.createLabel('Date:');
  var boxDate = app.createDateBox().setId('boxDate').setName('boxDate').setFormat(UiApp.DateTimeFormat.DATE_SHORT);

  var lbxSubject = app.createListBox().setId('lbxSubject').setName('lbxSubject');
  var lstSubjItems = ['-- Select One --','Algebra I (OCS)','Algebra II','Algebra II (Honors)','Agriscience Applications','Agriscience Applications (OCS)','Anatomy and Physiology','Anatomy and Physiology (Honors)','Animal Science','American Sign Language II','Apparel I','Art History (AP)','Advanced Functions and Modeling','Automotive Computer System Diagnostics','Automotive Brakes','Automotive Service','Automotive Service (OCS)', 'Benchmarks','Bible History','Biology','Biology (AP)','Biology (Honors)','Biology (OCS)','Biomedical Technology','Biotechnology and Agriscience','Calculus AB (AP)','Calculus BC (AP)','Carpentry I','Carpentry I (OCS)','Carpentry III','Chemistry','Chemistry (AP)','Chemistry (Honors)','Choral Arts (Beginning)','Civics and Economics','Civics and Economics (Honors)','Computer Programming I','Computer Science (AP)','Computer Technology','Concert Band II (Beginning)',        'Concert Band II (Proficient)','Core and Sustainable Construction','Core and Sustainable Construction (OCS)','Digital Media I','Digitial Media I (OCS)','Digital Media II','Discrete Mathematics','Drafting I','Drafting Architecture II (Honors)','Drafting Architecture III (Honors)','Earth and Environmental Science','Earth and Environmental Sceince (AP)','Earth and Environmental Science (Honors)','Early Childhood Ed I','eCommerce I (Honors)','English I','English I (Honors)','English II','English II (Honors)','English II (OCS)','English III','English III (Honors)','English III (OCS)','English IV','English IV (Honors)','English IV (OCS)','English Lang and Comp (AP)','English Language Arts II','English Language Arts III','Engish Literature (AP)','European History (AP)','Fashion Merchandising','Financial Management (OCS)','Foods I','Foods I (OCS)','Foods II - Enterprise','Foods II - Advanced (OCS)','French I','French II','French III (Honors)','Functional Finances','Geometry', 'Geometry (Honors)','German I','German II','German III (Honors)','Health and Physical Education','Health Sciences (Advanced Studies)','Health Team Relations','Human Geography (AP)','Holocaust and Peace Studies','Horticulture I','Horticulture I (OCS)','Horticulture II','Horticulture II (OCS)','Integrated Math I','Interior Design I','Introduction to Math','Japanese I','JROTC I','JROTC II','JROTC III','JROTC IV','Latin I','Latim II','Latin III (Honors)','Leadership Development','Marketing Management','Masonry I','Masonry I (OCS)','Masonry III','Microsoft Excel and Access','Microsoft Word PowerPoint and Publisher','Multimedia and Webpage Design','Music Business and Recording (Beginning)','Occupational Preparations I','Occupational Preparations II','Occupational Preparations III','Occupational Preparations IV','Parent and Child Development',        'Parent and Child Development (OCS)','Personal Finance',        'Personal Finance (OCS)','Physical Science','Physics (Honors)','Pre-Calculus (Honors)','Professional Management and Leadership I','Professional Management and Leadership II','Psychology','Physcology (AP)','Psychology (Honors)','SAT Preparation','Science and Technical Visualization I','Science and Technical Visualization II','Social Studies (OCS)','Spanish I','Spanish II','Spanish III (Honors)','Spanish IV (Honors)','Special Topics of Mathematics','Symphonic Band II (Beginning)','Symphonic Band II (Intermediate)','Symphonic Band II (Proficient)','Technology Engineering and Design','Theatre Arts (Beginning)','Theatre Arts (Intermediate)','US History','US History (AP)','US History (Honors)','Visual Arts (Beginning)','Visual Arts (Intermediate)','Visual Arts (Proficient)','Visual Arts (Advanced)','Visual Arts Specialization in Ceramics (Advanced)','Vocal Ensemble (Intermediate)','Vocal Ensemble (Proficient)','Wind Ensemble II (Intermediate)','Wind Ensemble II (Proficient)','Wind Ensemble II (Advanced)','World Geography','World History','World History','World History (Honors)'];
      lstSubjItems.sort();

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

  var lbxPeriod = app.createListBox().setId('lbxPeriod').setName('lbxPeriod');
  var lstPrdItems = ['-- Select One --','1st Period','2nd Period','3rd Period','4th Period'];
      lstPrdItems.sort();

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

  var lblStudentNum = app.createLabel('Number of Students:');
  var txtStudentNum = app.createTextBox().setName('txtStudentNum');

  var txtRadioGrp1 = app.createTextBox().setName('txtRadioGrp1').setVisible(false);
  var txtRadioGrp2 = app.createTextBox().setName('txtRadioGrp2').setVisible(false);
  var txtElementsID = app.createTextBox().setName('txtElementsID').setVisible(false);
  var txtQuiaLink = app.createTextBox().setName('txtQuiaLink').setVisible(false);
  var txtQuiaPass = app.createTextBox().setName('txtQuiaPass').setVisible(false);
  //Create Source Radio Button Group
  var radSource1 = app.createRadioButton('group1', 'Hard-Copy').setFormValue('Hard-Copy').addClickHandler(app.createClientHandler().forTargets(txtRadioGrp1).setText('Hard-Copy'));
  var radSource2 = app.createRadioButton('group1', 'Electronic-Copy').setFormValue('Electronic-Copy').addClickHandler(app.createClientHandler().forTargets(txtRadioGrp1).setText('Electronic-Copy'));
  //Create Type Radio Button Group
  var radSource3 = app.createRadioButton('group2', 'Teacher-Made Exam').setFormValue('Teacher-Made Exam').addClickHandler(app.createClientHandler().forTargets(txtRadioGrp2).setText('Teacher-Made Exam'));
  var radSource4 = app.createRadioButton('group2', 'Elements Exam').setFormValue('Elements Exam').addClickHandler(app.createClientHandler().forTargets(txtRadioGrp1).setText('Elements Exam'));
      if (radSource4 == true) {
          txtElementsID.setVisible(true);
      } else {
          txtElementsID.setVisible(false);
      }
  var radSource5 = app.createRadioButton('group2', 'Quia Exam').setFormValue('Quia Exam').addClickHandler(app.createClientHandler().forTargets(txtRadioGrp1).setText('Quia Exam'));
      if (radSource5 == true) {
          txtQuiaLink.setVisible(true);
          txtQuiaPass.setVisible(true);
      }  else {
          txtQuiaLink.setVisible(false);
          txtQuiaPass.setVisible(false);
      }
  var btnCreate = app.createButton('Create Event');

  //Create handler which will execute 'createEvents(e)' on clicking the button
  var evtHandler = app.createServerClickHandler('createEvents');
      evtHandler.addCallbackElement(vertMainPanel);
  //Add this handler to the button
      btnCreate.addClickHandler(evtHandler);

  //Add all the elemnts to the panel 
  vertMainPanel.add(lblTeacherName)
       .add(txtTeacherName)
       .add(lblExt)
       .add(txtExt)
       .add(lblDate)
       .add(boxDate)
       .add(lbxSubject)
       .add(lbxPeriod)
       .add(lblStudentNum)
       .add(txtStudentNum)
       .add(radSource1)
       .add(radSource2)
       .add(txtRadioGrp1)
       .add(radSource3)
       .add(radSource4)
       .add(txtElementsID)
       .add(radSource5)
       .add(txtQuiaLink)
       .add(txtQuiaPass)
       .add(txtRadioGrp2)
       .add(btnCreate)

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

  //Return the application
  return app;
}

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.txtStudentNum;
    var ssSource = e.parameter.txtRadioGrp1;
    var ssType = e.parameter.txtRadioGrp2;
    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";

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

    //Log the entries in a spreadsheet
    var sheet = SpreadsheetApp.openById('0Aur3owCpuUY-dGJIOGZ1LXhqT2FNMGVXSGNJazFnUmc#gid=0').getActiveSheet();//Change the spreadhseet key to yours
    var lastRow = sheet.getLastRow();
    var targetRange = sheet.getRange(lastRow+1, 1, 1, 10).setValues([[new Date(),eventDate,ssTeacher,ssExt,ssSubject,ssPeriod,ssSource,ssType,ssStudentNum,'Event created']]);
    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)

使用切换可见性的ClientHandler ...

一个简短的例子比一个很长的解释更好,我想这很清楚,只需在创建了所有的radioButtons和textBox后插入以下代码。

DEMO app和完整代码 here

  //Client Handlers for textBoxes
  var showTxtElementHandler = app.createClientHandler().forTargets(txtElementsID).setVisible(true);
  var hideTxtElementHandler = app.createClientHandler().forTargets(txtElementsID).setVisible(false);
  radSource4.addClickHandler(showTxtElementHandler);// show when click on radio4
  radSource3.addClickHandler(hideTxtElementHandler);// hide if click other
  radSource5.addClickHandler(hideTxtElementHandler);

  var showtxtQuiaLinkHandler = app.createClientHandler().forTargets(txtQuiaLink).setVisible(true);
  var hidetxtQuiaLinkHandler = app.createClientHandler().forTargets(txtQuiaLink).setVisible(false);
  radSource5.addClickHandler(showtxtQuiaLinkHandler);// show when clicked on radio5
  radSource3.addClickHandler(hidetxtQuiaLinkHandler);// hide in other cases
  radSource4.addClickHandler(hidetxtQuiaLinkHandler);

编辑:你的意思是这样吗? :

enter image description here

在这种情况下,我使用下面的水平面板(演示更新)

  var txtRadioGrp1 = app.createTextBox().setName('txtRadioGrp1').setVisible(false);
  var txtRadioGrp2 = app.createTextBox().setName('txtRadioGrp2').setVisible(false);
  var txtElementsID = app.createTextBox().setName('txtElementsID');
  var txtQuiaLink = app.createTextBox().setName('txtQuiaLink');
  var txtElementsIDP = app.createHorizontalPanel().add(app.createLabel('enter a value')).add(txtElementsID).setVisible(false);
  var txtQuiaLinkP = app.createHorizontalPanel().add(app.createLabel('enter a value')).add(txtQuiaLink).setVisible(false);
  var txtQuiaPass = app.createTextBox().setName('txtQuiaPass').setVisible(false);
  //Create Source Radio Button Group
  var radSource1 = app.createRadioButton('group1', 'Hard-Copy').setFormValue('Hard-Copy').addClickHandler(app.createClientHandler().forTargets(txtRadioGrp1).setText('Hard-Copy'));
  var radSource2 = app.createRadioButton('group1', 'Electronic-Copy').setFormValue('Electronic-Copy').addClickHandler(app.createClientHandler().forTargets(txtRadioGrp1).setText('Electronic-Copy'));
  //Create Type Radio Button Group
  var radSource3 = app.createRadioButton('group2', 'Teacher-Made Exam').setFormValue('Teacher-Made Exam').addClickHandler(app.createClientHandler().forTargets(txtRadioGrp2).setText('Teacher-Made Exam'));
  var radSource4 = app.createRadioButton('group2', 'Elements Exam').setFormValue('Elements Exam').addClickHandler(app.createClientHandler().forTargets(txtRadioGrp1).setText('Elements Exam'));
  var radSource5 = app.createRadioButton('group2', 'Quia Exam').setFormValue('Quia Exam').addClickHandler(app.createClientHandler().forTargets(txtRadioGrp1).setText('Quia Exam'));

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

  //Client Handlers for textBoxes
  var showTxtElementHandler = app.createClientHandler().forTargets(txtElementsIDP).setVisible(true);
  var hideTxtElementHandler = app.createClientHandler().forTargets(txtElementsIDP).setVisible(false);
  radSource4.addClickHandler(showTxtElementHandler);
  radSource3.addClickHandler(hideTxtElementHandler);
  radSource5.addClickHandler(hideTxtElementHandler);

  var showtxtQuiaLinkHandler = app.createClientHandler().forTargets(txtQuiaLinkP).setVisible(true);
  var hidetxtQuiaLinkHandler = app.createClientHandler().forTargets(txtQuiaLinkP).setVisible(false);
  radSource5.addClickHandler(showtxtQuiaLinkHandler);
  radSource3.addClickHandler(hidetxtQuiaLinkHandler);
  radSource4.addClickHandler(hidetxtQuiaLinkHandler);

编辑2:GRID版本

enter image description here

  //Add all the elemnts to the panel 
  var grid = app.createGrid(12,2).setCellPadding(5);
  vertMainPanel.add(grid);
  grid.setWidget(0,0,lblTeacherName)
  .setWidget(0,1,txtTeacherName)
  .setWidget(1,0,lblExt)
  .setWidget(1,1,txtExt)
  .setWidget(2,0,lblDate)
  .setWidget(2,1,boxDate)
  .setWidget(3,1,lbxSubject)
  .setWidget(3,0,lbxPeriod)
  .setWidget(4,0,lblStudentNum)
  .setWidget(4,1,txtStudentNum)
  .setWidget(5,0,radSource1)
  .setWidget(5,1,radSource2)
  .setWidget(6,0,txtRadioGrp1)
  .setWidget(7,0,radSource3)
  .setWidget(8,0,radSource4)
  .setWidget(8,1,txtElementsIDP)
  .setWidget(9,0,radSource5)
  .setWidget(9,1,txtQuiaLinkP)
  .setWidget(10,0,txtQuiaPass)
  .setWidget(10,1,txtRadioGrp2)
  .setWidget(11,0,btnCreate);