2014-09-23 155 views
0

我正试图创建一个用户在当天进入作业的GUI。提交后,表单会将数据发送到日历和电子表格。Google Apps脚本[createServerClickHandler]

Apps脚本告诉我'createServerClickHandler'已被弃用。还有什么其他的选择?

我的代码有什么问题吗?

//Create the GUI form 

function doGet() { 
    var app = UiApp.createApplication().setTitle('Form and Calendar Events'); 

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

    //Create the form elelemnts 
    var startDateLabel = app.createLabel('Homework Start Date:'); 
    var startDate = app.createDateBox().setId('startDate'); 

    var endDateLabel = app.createLabel('Homework End Date:'); 
    var endDate = app.createDateBox().setId('endDate'); 

    var hwTitleLabel = app.createLabel('Homework title:'); 
    var hwTitle = app.createTextBox().setName('hwTitle').setId('hwTitle'); 

    var hwDetailLabel = app.createLabel('Homework Details:'); 
    var hwDetails = app.createTextArea() 
     .setSize('200', '100').setId('hwDetail').setName('hwDetail'); 
    var btn = app.createButton('Submit Homework'); 

    //Create handler which will execute 'Submit Homework(e)' on clicking the button 
    var handler = app.createServerClickHandler('Submit Homework'); 
    handler.addCallbackElement(panel); 
    //Add this handler to the button 
    btn.addClickHandler(handler); 

    //Add all the elemnts to the panel 
    panel.add(startDateLabel) 
    .add(startDate) 

    .add(endDateLabel) 
    .add(endDate) 

    .add(hwTitleLabel) 
    .add(hwTitle) 

    .add(hwDetailLabel) 
    .add(hwDetails) 

    .add(btn); 
    //Add this panel to the application 
    app.add(panel); 
    //return the application 
    return app; 
} 

function createEvents(e){ 

    //Get the active application 
    var app = UiApp.getActiveApplication(); 

    try{ 
    //get the entries; 
    var startDate = e.parameter.startDate; 
    var endDate = e.parameter.endDate; 
    var hwTitle = e.parameter.hwTitle; 
    var hwDetails = e.parameter.hwDetails; 

    //Get the calendar 
    var cal = CalendarApp.getCalendarsByName('Paul Lim')[0];//Change the calendar name 
    var startDate = startDate; 
    var endDate = endDate; 
    //Create the events 
    cal.createEvent(hwTitle, startDate, endDate,{description:hwDetail}); 

    //Log the entries in a spreadsheet 
    var ss = SpreadsheetApp.openById('1PTSsvjsvb-UVuJ_z9WoLGVZ01YigjLQ5nCi7ujU4Gek');//Change the spreadhseet key to yours 
    var sheet = ss.getSheets()[0]; 
    sheet.getRange(sheet.getLastRow()+1, 1, 1, 5).setValues([[new Date(), endDate, hwTitle, hwDetails]]); 

    //Show the confirmation message 
    app.add(app.createLabel('Homework created successfully')); 
    //make the form panel invisible 
    app.getElementById('panel').setVisible(false); 
    return app; 
    } 

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

回答

0

使用createServerHandler而是......为了避免这种问题,我建议你使用的自动完成功能的脚本编辑器:如果你输入“创造”,打控制空间,你会看到所有的可用的方法,包括不推荐使用的方法。

您的代码中还存在另一个问题:您忘记将名称设置为您的日期小部件,它们将不会在服务器处理函数中返回任何值。

+0

嗨,谢谢你的回复。 我改成以下 '//创建处理程序将执行“提交(E)”上点击按钮 VAR处理机= app.createServerHandler(“提交”); handler.addCallbackElement(panel); //将此处理程序添加到按钮 btn.addClickHandler(处理程序);' 但是,它显示'未找到脚本函数:提交'。 – chopz 2014-09-23 13:16:13

+0

脚本中的函数名称是“createEvents”,将其用作createServerHandler中的参数。 – 2014-09-23 14:03:14