在Google Apps脚本中从服务器到客户端进行通信 [英] Communicate from server to client side in Google Apps script

查看:48
本文介绍了在Google Apps脚本中从服务器到客户端进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个具有客户端和服务器端组件的Google Apps脚本.客户端组件显示进度条.客户端调用服务器端功能(异步调用),这些功能的进度必须显示在客户端进度栏中.现在,我想要的是能够根据来自服务器端功能的反馈来更新客户端进度条.这可能吗?

I am trying to write a Google Apps script which has a client and server side component. The client side component displays a progress bar. The client calls server side functions (which are called asynchronously), whose progress has to be shown in the client side progress-bar. Now, what I want is to be able to update the client side progress bar based on feedback from the server side functions. Is this possible?

由于JS异步进行服务器端调用,因此造成了复杂性,因此我无法真正在客户端调用该函数并更新进度栏的循环.

The complexity is created due the the fact that JS makes the server-side calls asynchronously and hence I cannot really have a loop on the client side calling the functions and updating the progress bar.

我当然可以将服务器端功能的执行分成多个步骤,从客户端一个接一个地调用,每次更新状态栏.但是我想知道是否有更好的解决方案.有没有一种方法可以从服务器端调用客户端函数,并根据传递的参数来更新进度条?还是有办法从服务器端访问客户端进度栏对象并对其进行修改?

I could of course split up the execution of the server side function in multiple steps, call one by one from the client side, each time updating the status bar. But I'm wondering if there's a better solution. Is there a way to call a client side function from the server side, and have that update the progress bar based on the argument passed? Or is there a way to access the client side progress-bar object from server side and modify it?

推荐答案

我建议您将这两个问题分开,而不是将工作请求和进度更新捆绑在一起.

Rather than tying the work requests and progress updating together, I recommend you separate those two concerns.

在服务器端,应客户端请求执行工作的功能应更新状态存储;例如,这可能是ScriptProperty.在工作功能完成工作之前,他们不需要响应客户.服务器还应该具有一个可以由客户端调用的功能,以简单地报告当前进度.

On the server side, functions that are performing work at the request of the client should update a status store; this could be a ScriptProperty, for example. The work functions don't need to respond to the client until they have completed their work. The server should also have a function that can be called by the client to simply report the current progress.

当客户端第一次调用服务器以请求工作时,它还应该调用进度报告器.(大概是,第一次调用将得到 0%的结果.)状态调用的 onSuccess 处理程序可以更新您用来表示进度的任何视觉效果,然后调用服务器的进度报告程序再次作为自身的成功处理程序.当然,应该延迟进行此操作.

When the client first calls the server to request work, it should also call the progress reporter. (Presumably, the first call will get a result of 0%.) The onSuccess handler for the status call can update whatever visual you're using to express progress, then call the server's progress reporter again, with itself as the success handler. This should be done with a delay, of course.

当进度达到 100%或工作完成时,可以关闭客户端的进度检查器.

When progress reaches 100%, or the work is completed, the client's progress checker can be shut down.

这篇关于在Google Apps脚本中从服务器到客户端进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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