Google script : how to close an ui from the serverhandler, while another function is running

拥有回忆 提交于 2019-12-13 03:44:51

问题


I'm trying to open an UI, that's asking the user for two variable, and on "ok", close the ui, and execute a function with that two variables as parameter...

My problem is that the ui remains open, and the code from the serverhandler is executing in a loop until the ui is open. If I don't see any loop in my code, if I execute the same function directly without ui, it is doing well... I miss something on my handler ?

 function doGet() {
  var app = UiApp.createApplication();

  var mygrid = app.createGrid(3, 2);
  mygrid.setWidget(0, 0, app.createLabel('Date de début:'));
  mygrid.setWidget(0, 1, app.createDateBox().setId('dateA'));
  mygrid.setWidget(1, 0, app.createLabel('Date de fin:'));
  mygrid.setWidget(1, 1, app.createDateBox().setId('dateB'));

  var mybutton = app.createButton('OK');
  mybutton.setId("mybutton");
  var mypanel = app.createVerticalPanel();
  mypanel.setId('mypanel');
  mypanel.add(mygrid); 
  mypanel.add(mybutton);
  app.add(mypanel);

  var handler = app.createServerHandler('summaryOnDemand');
  handler.addCallbackElement(mypanel);
  mybutton.addClickHandler(handler);

  return app;
}

function summaryOnDemand(e) {
  var app = UiApp.getActiveApplication();
  var dateA = e.parameter.dateA;
  var dateB = e.parameter.dateB;
  var panel = app.getElementById('mypanel');
  panel.setVisible(false);
  app.close();
 //   var mydoc = SpreadsheetApp.openById("0AiK_IybPWcGcdEV5V2JQUHhtUHp1dEhEN3NuQjdrVWc");

  var file = summary(dateA,dateB,true);
  var folder = DocsList.getFolderById("0ByK_IybPWcGcX1lUTlRET0dQVXc");
  file.addToFolder(folder);
  MailApp.sendEmail(Session.getActiveUser().getEmail(),"Rapport réclamation à la demande","Le document ce trouve dans google drive à présent, pour y accéder directement : " + file.getUrl())


  return app;
}

回答1:


You can not close UI in response to a handler in UiApp. UiApp can not interact with Browser objects. app.close; will work only if the Apps Script in contained in a spreadsheet and it is not published as a service. You modified code should look like this

 function doGet() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
  var app = UiApp.createApplication();

  var mygrid = app.createGrid(3, 2);
  mygrid.setWidget(0, 0, app.createLabel('Date de début:'));
  mygrid.setWidget(0, 1, app.createDateBox().setId('dateA'));
  mygrid.setWidget(1, 0, app.createLabel('Date de fin:'));
  mygrid.setWidget(1, 1, app.createDateBox().setId('dateB'));

  var mybutton = app.createButton('OK');
  mybutton.setId("mybutton");
  var mypanel = app.createVerticalPanel();
  mypanel.setId('mypanel');
  mypanel.add(mygrid); 
  mypanel.add(mybutton);
  app.add(mypanel);

  var handler = app.createServerHandler('summaryOnDemand');
  handler.addCallbackElement(mypanel);
  mybutton.addClickHandler(handler);

  doc.show(app);
}



回答2:


Waqar's answer is indeed correct, you cannot close an UI when it is running as a webapp but if the solution he proposes to switch to a 'spreadsheet mode' doesn't meet your requirements there is another possibility.

In short, you can set all elements 'invisible' and show a label that confirms the execution of the user's request. Here is a possible code to achieve that :

 function doGet() {
  var app = UiApp.createApplication();

  var mygrid = app.createGrid(3, 2);
  mygrid.setWidget(0, 0, app.createLabel('Date de début:'));
  mygrid.setWidget(0, 1, app.createDateBox().setId('dateA'));
  mygrid.setWidget(1, 0, app.createLabel('Date de fin:'));
  mygrid.setWidget(1, 1, app.createDateBox().setId('dateB'));

  var mybutton = app.createButton('OK');
  mybutton.setId("mybutton");
  var mypanel = app.createVerticalPanel();
  mypanel.setId('mypanel');
  mypanel.add(mygrid);
  var label = app.createHTML('<BR><BR>Your request is being processed, <BR><BR>thanks for submitting').setId('Label').setStyleAttribute('padding', '15').setVisible(false).setWidth('300').setHeight('300');// adjust dimensions to your needs  
  mypanel.add(label)
  mypanel.add(mybutton);
  app.add(mypanel).add(label);

  var handler = app.createServerHandler('summaryOnDemand');
  handler.addCallbackElement(mypanel);
  mybutton.addClickHandler(handler);
   var Chandler = app.createClientHandler();
   Chandler.forTargets(mygrid,mybutton).setVisible(false)
   mybutton.addClickHandler(Chandler)        
  return app;
}

function summaryOnDemand(e) {
  var app = UiApp.getActiveApplication();
  var Label = app.getElementById('Label');
  Label.setVisible(true);// show the masking label with message
  var dateA = e.parameter.dateA;
  var dateB = e.parameter.dateB;
  return app
}


来源:https://stackoverflow.com/questions/11314772/google-script-how-to-close-an-ui-from-the-serverhandler-while-another-functio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!