单选按钮:无法获得选择

时间:2013-09-25 15:23:34

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

在下面的代码中,我希望能够在日历事件中返回单选按钮值,以便人们知道选择了哪个操作... 我尝试了几种不同的选项但是没有似乎工作。我不断收到错误,必须编写大量没有结果的代码。 最简单的解决方案是什么?

由于

//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');

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

  var lbxPeriod = app.createListBox().setId('lbxPeriod').setName('lbxPeriod');
        lbxSubject.setVisibleItemCount(1);
        lbxPeriod.addItem('-- Select One --');
        lbxPeriod.addItem('1st Period');
        lbxPeriod.addItem('2nd Period');
        lbxPeriod.addItem('3rd Period');
        lbxPeriod.addItem('4th Period');

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

  var radSource1 = app.createRadioButton('group1', 'Hard-Copy').setFormValue('Hard-Copy').setName('Hard-Copy').setId('Hard-Copy');
  var radSource2 = app.createRadioButton('group1', 'Electronic-Copy').setFormValue('Electronic-Copy').setName('Electronic-Copy').setId('Electronic-Copy');

  var radSource3 = app.createRadioButton('group2', 'Teacher-Made Exam').setFormValue('Teacher-Made Exam').setName('Teacher-Made').setId('Teacher-Made');
  var radSource4 = app.createRadioButton('group2', 'Elements Exam').setFormValue('Elements Exam').setName('Elements').setId('Elements');
  var radSource5 = app.createRadioButton('group2', 'Quia Exam').setFormValue('Quia Exam').setName('Quia').setId('Quia');

  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(radSource3)
       .add(radSource4)
       .add(radSource5)
       .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 = "";
    var ssType = "";
    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, Utilities.formatDate(eventDate, 'EST', 'MM/DD/YYYY'), {description:eventCalDetails});

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

    //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)

RadioButtons在GAS UI中有点棘手,它们必须具有相同的名称才能像radioButton一样运行,但这使得无法像往常一样使用e.parameter.name获取它们的值...(另请参阅issue 506example here

因此,解决方案是使用客户端处理程序,当您单击radioButton并在每个组中为它们指定相同名称时,为(隐藏)textBox分配值。

看起来有点复杂(或者我说的不太好)所以我为你编写了代码。

以下是您的代码的相关修改部分和a working demo(为了演示目的,我将两个文本框都可见,但您当然应该使用setVisible(false)隐藏它们

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

  var radSource1 = app.createRadioButton('group1', 'Hard-Copy').setFormValue('Hard-Copy').addClickHandler(app.createClientHandler().forTargets(radioValGroup1).setText('hardCopy'));
  var radSource2 = app.createRadioButton('group1', 'Electronic-Copy').setFormValue('Electronic-Copy').addClickHandler(app.createClientHandler().forTargets(radioValGroup1).setText('Electronic-Copy'));

  var radSource3 = app.createRadioButton('group2', 'Teacher-Made Exam').setFormValue('Teacher-Made Exam').addClickHandler(app.createClientHandler().forTargets(radioValGroup2).setText('Teacher-Made Exam'));
  var radSource4 = app.createRadioButton('group2', 'Elements Exam').setFormValue('Elements Exam').addClickHandler(app.createClientHandler().forTargets(radioValGroup2).setText('Elements Exam'));
  var radSource5 = app.createRadioButton('group2', 'Quia Exam').setFormValue('Quia Exam').addClickHandler(app.createClientHandler().forTargets(radioValGroup2).setText('Quia Exam'));

  var btnCreate = app.createButton('Create Event');
  vertMainPanel.add(radioValGroup1).add(radioValGroup2)
  //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);

      vertMainPanel.add(radioValGroup1).add(radioValGroup2);// add 2 textBoxes to panel=callBackElement
      ...

在您的服务器处理函数中,您只需使用e.parameter.radioValGroup1e.parameter.radioValGroup2检索这些值,就像任何其他小部件一样。


编辑:另外,如果我可以建议一个小的修改,用一个for循环迭代一个数组来填充你的列表会更容易:

  var lbxSubject = app.createListBox().setId('lbxSubject').setName('lbxSubject').setVisibleItemCount(5);
  var listItems = ['-- Select One --','Algebra I (OCS)','Algebra II','Algebra II (Honors)','Agriscience Applications','Agriscience Applications (OCS)','Anatomy and Physiology','Anatomy and Physiology (Honors)','and so on...'];
  listItems.sort();
  for(var l=0;l<listItems.length;++l){
  lbxSubject.addItem(listItems[l]);
  }

维护起来会更容易(如果您需要添加/删除项目,不需要关心订单,因为它会自动排序)并且代码更紧凑......