单击时如何禁用按钮?

时间:2014-07-03 09:08:22

标签: google-apps-script

我使用以下代码

创建表单
var app = UiApp.createApplication();
app.setTitle('My Title');
app.setHeight(150);
app.setWidth(300);

var form = app.createFormPanel();
var flow = app.createFlowPanel();

flow.add(app.createHidden("action", action));
flow.add(app.createLabel('My Label'));

//Submit
var submit = app.createButton('Run', app.createServerHandler('myFunction').addCallbackElement(form)).setId('run');
flow.add(submit);

form.add(flow);
app.add(form);

//return app;
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
spreadsheet.show(app);

myFunction按钮点击处理程序中,我尝试使用以下代码引用我的按钮

function myFunction(e) {
  var app = UiApp.getActiveApplication();
  var submitButton = app.getElementById('run');
  try{
    submitButton.setEnabled(false);
  }finally{
    submitButton.setEnabled(true);
  }
}

但它不起作用。我得到以下执行日志(不是Logger.log日志记录,但我的意思是可以从查看>执行记录中显示的执行日志记录)

[14-07-03 11:04:44:091 SAST] Starting execution
[14-07-03 11:04:44:124 SAST] UiApp.getActiveApplication() [0 seconds]
[14-07-03 11:04:44:125 SAST] (class).getElementById([fetch]) [0 seconds]
[14-07-03 11:04:44:125 SAST] (class).setEnabled([false]) [0 seconds]
[14-07-03 11:04:44:126 SAST] (class).setEnabled([true]) [0 seconds]
[14-07-03 11:04:44:127 SAST] Execution succeeded [0.002 seconds total runtime]

但按钮从不被禁用!如何在单击处理程序被触发时禁用它?

1 个答案:

答案 0 :(得分:3)

您可以使用clientHandler更简单地执行此操作(并且更可靠,因为客户端处理程序会立即做出反应!)

只需将其添加到按钮中即可:

  var app = UiApp.createApplication();
  app.setTitle('My Title');
  app.setHeight(150);
  app.setWidth(300);

  var form = app.createFormPanel();
  var flow = app.createFlowPanel();

  flow.add(app.createHidden("action", 'action'));
  flow.add(app.createLabel('My Label'));

  //Submit
  var submit = app.createButton('Run', app.createServerHandler('myFunction').addCallbackElement(form)).setId('run');
  var cliHandler = app.createClientHandler().forEventSource().setEnabled(false);
  submit.addClickHandler(cliHandler);
  flow.add(submit);

  form.add(flow);
  app.add(form);
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  spreadsheet.show(app);

另请注意,表单通常使用submitButton按预期工作...阅读the doc about it

编辑:在服务器处理函数中重新启用:

var app = UiApp.getActiveApplication();
app.getElementById('run').setEnabled(true);
return app;