使用回报程序;没有结束目前的功能 [英] use return app; without ending current function

查看:85
本文介绍了使用回报程序;没有结束目前的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有另一种方法来更新gui,而不是返回应用程序;



我想设置文本在进行网址抓取之前,就像开始下载一样,并在完成后将标签转换为下载完成。

 函数EventHandler (e){
var app = UiApp.getActiveApplication();
var url = e.parameter.URLInput;
var label = app.getElementById(label);
label.setText(开始下载);
尝试{
var file = UrlFetchApp.fetch(url).getBlob();
} catch(err){
label.setText(err);
}
label.setText(下载完成);
返回应用程序;
}

标签保持为空,直到 UrlFetchApp 完成,然后标签的内容是'下载完成'。在获取结束函数前添加返回应用程序;

您必须使用 clientHandler 将Text设置为您的标签doGet函数,当你点击按钮时clientHandler立即执行。



这是一个测试应用程序,它显示了它的工作原理:(此处提供的在线测试以及模拟下载)

 函数doGet(){
var app = UiApp.createApplication();
var label = app.createLabel('--- empty ---')。setId('label');
app.add(label)
var handler = app.createServerHandler('EventHandler');
var cHandler = app.createClientHandler()。forTargets(label).setText('starting download');
var btn = app.createButton('start',handler).addClickHandler(cHandler);
app.add(btn);
返回应用程序;
}



函数EventHandler(e){
var app = UiApp.getActiveApplication();
var url = e.parameter.URLInput;
var ulabel = app.getElementById(label);
ulabel.setText(开始下载);
尝试{
// var file = UrlFetchApp.fetch(url).getBlob();
} catch(err){
label.setText(err);
}
ulabel.setText(下载完成);
返回应用程序;

$ / code>

注意:您可以使用相同的客户端处理程序来执行大量其他有用的事情:禁用按钮,显示一个微调器......任何你喜欢的东西都必须在doGet函数中发生,而不会延迟。




编辑关注您的评论



您是否尝试过并行使用2个服务器处理程序?在displayHandler中,你可以设置你想要的任何条件,我在下面的例子中简化了它:

  function doGet(){
var app = UiApp.createApplication();
var label = app.createLabel('--- empty ---')。setId('label');
app.add(label)
var handler = app.createServerHandler('EventHandler');
var displayHandler = app.createServerHandler('displayHandler');
var btn = app.createButton('start',handler).addClickHandler(displayHandler);
//你可以添加其他处理程序(按键,悬停......任何),它们将同时执行
app.add(btn);
返回应用程序;
}

function displayHandler(e){
var app = UiApp.getActiveApplication();
var ulabel = app.getElementById(label);
ulabel.setText(开始下载);
返回应用程序;
}

函数EventHandler(e){
var app = UiApp.getActiveApplication();
var url = e.parameter.URLInput;
var ulabel = app.getElementById(label);
尝试{
Utilities.sleep(2000); //模拟下载
} catch(err){
label.setText(err);
}
ulabel.setText(下载完成);
返回应用程序;
}


Is there another way to update the gui than return app;?

I want to set the text on a label before doing an url fetch, like started downloading, and after it completes turn the label into download complete.

function EventHandler(e) {
  var app = UiApp.getActiveApplication();
  var url = e.parameter.URLInput;
  var label = app.getElementById("label");
  label.setText("Download started");
  try{
    var file = UrlFetchApp.fetch(url).getBlob();
   } catch(err){
    label.setText(err);
  }
  label.setText("Download finished");
  return app;
}

The label stays empty until UrlFetchApp is completed, and then the label's content is 'Download finished'. Adding return app; before the fetch ends the function.

解决方案

You have to use a clientHandler to set Text to your label in the doGet function, the clientHandler executes immediately when you click the button.

Here is a test app that shows how it works: (online test available here with simulated download)

function doGet(){
  var app = UiApp.createApplication();
  var label = app.createLabel('---empty---').setId('label');
  app.add(label)
  var handler = app.createServerHandler('EventHandler');
  var cHandler = app.createClientHandler().forTargets(label).setText('starting download');
  var btn = app.createButton('start',handler).addClickHandler(cHandler);
  app.add(btn);
  return app;
}



function EventHandler(e) {
  var app = UiApp.getActiveApplication();
  var url = e.parameter.URLInput;
  var ulabel = app.getElementById("label");
  ulabel.setText("Download started");
  try{
    //var file = UrlFetchApp.fetch(url).getBlob();
  } catch(err){
    label.setText(err);
  }
  ulabel.setText("Download finished");
  return app;
}

note : you can use the same client handler to do lots of other usefull things : disable the button, show a spinner... whatever you like that must happen in the doGet function without delay.


EDIT following your comment

Have you tried using 2 server handlers in parallel ? in the displayHandler you could setup any condition you want, I left it simple in the following example :

function doGet(){
  var app = UiApp.createApplication();
  var label = app.createLabel('---empty---').setId('label');
  app.add(label)
  var handler = app.createServerHandler('EventHandler');
  var displayHandler = app.createServerHandler('displayHandler');
  var btn = app.createButton('start',handler).addClickHandler(displayHandler);
 // you can add other handlers (keypress, hover... whatever) they will all execute at the same time
  app.add(btn);
  return app;
}

function displayHandler(e) {
  var app = UiApp.getActiveApplication();
  var ulabel = app.getElementById("label");
  ulabel.setText("Download started");
  return app;
}

function EventHandler(e) {
  var app = UiApp.getActiveApplication();
  var url = e.parameter.URLInput;
  var ulabel = app.getElementById("label");
  try{
    Utilities.sleep(2000);// simulating download
  } catch(err){
    label.setText(err);
  }
  ulabel.setText("Download finished");
  return app;
}

这篇关于使用回报程序;没有结束目前的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆