单击按钮时:遇到错误:发生意外错误

时间:2015-06-21 17:10:06

标签: google-apps-script

我收到此错误"遇到错误:发生意外错误"当我点击创建的按钮时。

这是我的剧本:



function doGet(e) {
  var doc = SpreadsheetApp.openById('1DhMs7Ii2s0yl9V7-3Oeu9vymu3_Tnx8GaKSxEmNmdco');
  var app = UiApp.createApplication().setTitle('ADX Prod Tracker');

  var grid = app.createGrid(3, 4);
  grid.setWidget(0, 0, app.createLabel('Name:'));
  grid.setWidget(0, 1, app.createTextBox().setWidth(150).setId('userName').setName('userName').setText(Session.getActiveUser().getEmail()));
  grid.setWidget(1, 0, app.createLabel('Start / End Time:'));
  grid.setWidget(1, 1, app.createTextBox().setWidth(150).setId('start').setName('start').setText(Utilities.formatDate(new Date(), "GMT+08", "MM/dd/yyyy HH:mm:ss")));
  grid.setWidget(1, 2, app.createTextBox().setWidth(150).setId('end').setName('end'));

  var addButton = app.createButton('Time End');
  grid.setWidget(1, 3, addButton);
  var timeHandler = app.createServerClickHandler('time');
  addButton.addClickHandler(timeHandler);

  grid.setWidget(2, 0, app.createLabel('Activity:'));
  grid.setWidget(2, 1, app.createListBox().setName('list').setVisibleItemCount(2).addItem('Attendance').addItem('Training'));

  var panel = app.createVerticalPanel();
  panel.add(grid);

  var button = app.createButton('Submit');
  var submitHandler = app.createServerClickHandler('submit');
  submitHandler.addCallbackElement(grid);
  button.addClickHandler(submitHandler);
  panel.add(button);

  app.add(panel);
  return app;
}

function time() {
  var app = UiApp.getActiveApplication().getElementById('end');
  app.setValue(Utilities.formatDate(new Date(), "GMT+08", "MM/dd/yyyy HH:mm:ss"));

  return app;
}

function submit(e) {
  var doc = SpreadsheetApp.openById('1DhMs7Ii2s0yl9V7-3Oeu9vymu3_Tnx8GaKSxEmNmdco');
  var lastRow = doc.getLastRow();
  var cell = doc.getRange('a1').offset(lastRow, 0);
  cell.setValue(e.parameter.userName);
  cell.offset(0, 1).setValue(e.parameter.start);
  cell.offset(0, 2).setValue(e.parameter.end);
  cell.offset(0, 3).setValue(e.parameter.list);

  var app = UiApp.getActiveApplication();
  app.getElementById('userName').setValue('');
  app.getElementById('start').setValue('');
  app.getElementById('end').setValue('');

  return app;
}




我不知道我哪里出错了!有点新的,所以任何帮助都会很棒!

1 个答案:

答案 0 :(得分:0)

虽然我是UiApp的忠实粉丝,但不建议用它开始新项目,因为它已被正式弃用并可能停止工作。如果您正处于学习过程中,这将适用更多..

那就是说,下面是工作代码:(这些错误通常来自处理函数)

function doGet(e) {
//  var doc = SpreadsheetApp.openById('1DhMs7Ii2s0yl9V7-3Oeu9vymu3_Tnx8GaKSxEmNmdco');// not used here
  var app = UiApp.createApplication().setTitle('ADX Prod Tracker');
  var grid = app.createGrid(3, 4);
  grid.setWidget(0, 0, app.createLabel('Name:'));
  grid.setWidget(0, 1, app.createTextBox().setWidth(150).setId('userName').setName('userName').setText(Session.getActiveUser().getEmail()));
  grid.setWidget(1, 0, app.createLabel('Start / End Time:'));
  grid.setWidget(1, 1, app.createTextBox().setWidth(150).setId('start').setName('start').setText(Utilities.formatDate(new Date(), "GMT+08", "MM/dd/yyyy HH:mm:ss")));
  grid.setWidget(1, 2, app.createTextBox().setWidth(150).setId('end').setName('end'));

  var addButton = app.createButton('Time End');
  grid.setWidget(1, 3, addButton);
  var timeHandler = app.createServerClickHandler('time');
  addButton.addClickHandler(timeHandler);
  grid.setWidget(2, 0, app.createLabel('Activity:'));
  grid.setWidget(2, 1, app.createListBox().setName('list').setVisibleItemCount(2).addItem('Attendance').addItem('Training'));
  var panel = app.createVerticalPanel();
  panel.add(grid);
  var button = app.createButton('Submit');
  var submitHandler = app.createServerClickHandler('submit');
  submitHandler.addCallbackElement(grid);
  button.addClickHandler(submitHandler);
  panel.add(button);
  app.add(panel);
  return app;
}

function time() {
  var app = UiApp.getActiveApplication();
  app.getElementById('end').setText(Utilities.formatDate(new Date(), "GMT+08", "MM/dd/yyyy HH:mm:ss"));

  return app;
}

function submit(e) {
  var doc = SpreadsheetApp.openById('1DhMs7Ii2s0yl9V7-3Oeu9vymu3_Tnx8GaKSxEmNmdco');
  var lastRow = doc.getLastRow();
  var cell = doc.getRange('a1').offset(lastRow, 0);
  cell.setValue(e.parameter.userName);
  cell.offset(0, 1).setValue(e.parameter.start);
  cell.offset(0, 2).setValue(e.parameter.end);
  cell.offset(0, 3).setValue(e.parameter.list);

  var app = UiApp.getActiveApplication();
  app.getElementById('userName').setText('');
  app.getElementById('start').setText('');
  app.getElementById('end').setText('');

  return app;
}