简体   繁体   English

Google Apps脚本[createServerClickHandler]

[英]Google Apps Script [createServerClickHandler]

I'm trying to create a GUI where user enters homework for the day. 我正在尝试创建一个GUI,用户可以在其中输入当天的作业。 On submit, form will send data to Calendar and Spreadsheet. 提交后,表单会将数据发送到日历和电子表格。

Apps Script is telling me that 'createServerClickHandler' is deprecated. Apps脚本告诉我'createServerClickHandler'已过时。 What other alternatives are there? 还有什么其他选择?

Is there anything wrong with my codes? 我的代码有什么问题吗?

//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;
  }
}

Use createServerHandler instead... to avoid this kind of problems I'd suggest you use the autocomplete feature in the script editor: if you type "create" and hit control-space you'll see all the available methods, including the deprecated ones being stroked through. 请改用createServerHandler ...以避免此类问题,我建议您在脚本编辑器中使用自动完成功能:如果键入“ create”并点击控件空间,您将看到所有可用的方法,包括不赞成使用的方法被抚摸着。

There is also another issue in your code: you forgot to set names to your date Widgets, they won't return any value in the server handler function. 您的代码中还有另一个问题:您忘记将名称设置为日期Widget,它们在服务器处理程序函数中不会返回任何值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM