在服务器端执行Google Apps脚本异步功能 [英] Google Apps Script Async function execution on Server side

查看:72
本文介绍了在服务器端执行Google Apps脚本异步功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GMail插件,它使用CardService用于UI. Card操作的某些回调函数耗时超过30秒.因此,出现以下错误.

I have a GMail add-on which uses CardService for UI. Some of the callback functions for the Card actions take over than 30 sec. Thus, I'm getting the following error.

Gmail无法执行此附加操作.

Gmail could not perform this add-on action.

可以通过任何方式在服务器端以异步方式运行Google Apps脚本功能,因此我可以向用户返回一些通知,并在后台继续工作.

Is there, any way to run Google Apps Script functions on the Server side asynchronous way, so I can return to a user some notification and continue work behind the scenes.

我已经尝试使用某些类似这样的库一个但没有运气,我可以在语法上使用Promises,但在功能上仍然是同步的.

I have tried using some libraries like this one but with no luck, I'm able to use syntactically the Promises but functionally it's still synchronous.

推荐答案

到目前为止,Gmail附加组件还没有异步执行.即使有什么事情,也没有用户操作就无法刷新UI.

As of now, there is no asynchronous execution for the Gmail add-on. Even if there is something, there is no way to refresh the UI without user action.

但是,有一个hack.您可以做的是,如果有一个长期运行的过程,只需创建一个"openlink"操作(设置链接),即可打开一个网址( https://yourhtmlpageurl?redirect_uri = ),并带有html响应.此html可以进行jquery ajax调用,这可能要等待一段时间.在html窗口中获得响应后,将页面重定向到通过传递响应数据而传递的redirect_uri.因此,我们的插件将获得一个以json对象为参数的函数回调,并将所有查询参数传递给redirect_uri.获得预期的响应后,请使用 CacheService .返回一些html成功模板,该模板将自动自动关闭窗口.

But, there is a hack. What you can do is, if there is a long running process is there, just create a "openlink" action (set link), which should open a url (https://yourhtmlpageurl?redirect_uri=) with html response. This html can have jquery ajax call, which can wait for some time. Once you get the response in the html window, redirect the page to the redirect_uri that is passed by passing the response data. So, our add on will get a callback to the function with parameter as json object with all the query parameters to the redirect_uri. Once you get the expected response, cache the response by using CacheService. return some html success template, which should automatically close the window by itself.

用于创建 openlink 操作:

用于生成带有状态标记的重定向脚本URI:

For generating redirect script URI with state token:

function generateNewStateToken(callbackName, user_info) {
return ScriptApp.newStateToken()
.withMethod(callbackName)
.withArgument("user_info", JSON.stringify(user_info))
.withTimeout(3600)
.createToken();
}

function getRedirectURI() {
    return "https://script.google.com/macros/d/" + ScriptApp.getScriptId() + "/usercallback";
}

var htmlUrl = <your_html_url> + "?redirect_uri="+ getRedirectURI() + "&state=" + generateNewStateToken("asyncCallback", {});

CardService.newOpenLink().setUrl(htmlUrl).setOpenAs(CardService.OpenAs.OVERLAY).setOnClose(CardService.OnClose.RELOAD_ADD_ON);

function asyncCallback(data) {
    data.response; // response which you can pass from script
    CacheService.getUserCache().put("long_running_process_resp", data.response);
    data.user_info; // user_info passed during state token creation
    // cache the data using cache service
    return HtmlService.createHtmlOutputFromFile("success");
}

success.html :

<!DOCTYPE html>
<html>

  <head>
    <base target="_top">
    <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
  </head>
  <body>
    <div class="sidebar">
        <p>Long running Process completed.</p>
    </div>
  </body>
  <script>
  setTimeout(function() {
    top.window.close();
  }, 2000);
  </script>
</html>

一旦成功关闭了success.html,将刷新gmail插件.因此,您可以从CacheService查找长期运行的响应数据.

Once the success.html is closed by itself, there will be a refresh of the gmail add on. So, you can lookup for the long running response data from CacheService.

如果您对此过程还有其他疑问,请告诉我.

Let me know if you have any more questions on this process.

这篇关于在服务器端执行Google Apps脚本异步功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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