2013-09-25 114 views
0

在下面的代碼中,我希望單選按鈕值能夠在日曆事件中返回,以便人們知道選擇了哪個動作...我嘗試了幾種不同的方法選項,但似乎沒有任何工作。我不斷收到錯誤,不得不寫很多沒有結果的代碼。 什麼是最簡單的解決方案?單選按鈕:無法獲得選擇

感謝

//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('[email protected].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; 
    } 
} 

回答

0

單選按鈕是有點氣UI棘手,它們必須具有相同的名稱的行爲類似於單選但是這使得它無法使用e.parameter.name像往常一樣得到他們的價值。 ..(另見issue 506example here

所以解決的辦法就是使用分配一個值(隱藏)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]); 
    } 

這將是更容易維護(如果有的話,你需要添加/刪除一個項目,沒必要在意順序,因爲它會自動排序),更緊湊在你的代碼...

+0

謝謝你很幫忙...解決方案工作得很好,我改變了下拉列表到數組,並幫助清理我的代碼,並保存我的工作,如果我曾經添加主題。 –