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('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;
  }
}
4

1 に答える 1

0

RadioButton は、GAS UI では少しトリッキーです。radioButton のように動作するには同じ名前が必要ですが、これにより、通常どおり e.parameter.name を使用して値を取得することができなくなります... ( issue 506例 hereも参照してください)

したがって、解決策は、各グループで同じ名前を付けながら、radioButton をクリックしたときに (非表示の) textBox に値を割り当てるクライアント ハンドラーを使用することです。

少し複雑に思えるかもしれません (または、あまり説明していないかもしれません) ので、コードを書きました。

これは、コードの関連する変更部分と動作するデモです (デモ目的で両方のテキストボックスを表示したままにしましたが、もちろん、次を使用してそれらを非表示にする必要があります)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.radioValGroup1を使用してこれらの値を簡単に取得できます。e.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]);
  }

メンテナンスが簡単になり(アイテムを追加/削除する必要がある場合は、自動的にソートされるため、順序を気にする必要はありません)、コードがよりコンパクトになります...

于 2013-09-25T18:37:41.943 に答える