简体   繁体   English

检查功能是否正在与Google Apps脚本和Google Web Apps一起运行

[英]Check if function is running with Google Apps Script and Google Web Apps

I've written a simple Google Apps Script that connects to IFTTT on the smartphone and sends SMS. 我编写了一个简单的Google Apps脚本,该脚本可连接到智能手机上的IFTTT并发送短信。 Now I'm trying to develop a user-friendly interface by deploying the script as a Google Web App: the idea is that you should click a button, then it should read "Sending SMS" and keep you waiting until done. 现在,我正在尝试通过将脚本部署为Google Web App来开发用户友好的界面:想法是您应该单击一个按钮,然后它应该显示为“ SMS发送中”,并让您等待完成。

I think this needs the button code to check whether the script is running or not (thus it's terminated), but I can't get it to work. 我认为这需要按钮代码来检查脚本是否正在运行(因此已终止),但是我无法使其正常工作。
Here's my setup. 这是我的设置。

In code.gs I have the main function that sends SMS: code.gs中,我具有发送SMS的主要功能:

var isRunning;
function execute() {
 isRunning = true;
 if (checkRunning() == true) {Logger.log('isRunning: TRUE')} else {Logger.log('isRunning: FALSE')}
 OTHER STUFF;
 isRunning = false;
 if (checkRunning() == true) {Logger.log('isRunning: TRUE')} else {Logger.log('isRunning: FALSE')}
}

and the function that checks whether the variable isRunning is true or false : 以及检查变量isRunningtrue还是false的函数:

function checkRunning() {
  if (isRunning == true) {
    return true}
    else {return false}
}

In my index.html file there's a button to execute, and a button to check: 在我的index.html文件中,有一个按钮要执行,还有一个按钮要检查:

<input class="button" type="button" value="Send SMS" onclick="google.script.run.execute()"/>
<input class="btncheck" type="button" value="Check running"/>

with this code between <script></script> (using Jquery): <script></script>之间使用以下代码(使用Jquery):

var c;
setInterval(function() {google.script.run.withSuccessHandler(varSet).checkRunning()},1000);
function varSet(value) {c = value}

$('.btncheck').on('click', function() {
   alert();
 }); 

function alert() {
   alert(c)
}

I know for sure that the execute() function runs for about 15 seconds. 我肯定知道execute()函数运行大约15秒钟。 Therefore if I click the "execute" button, in the following seconds I should be able to click the "check running" button and output true . 因此,如果我单击“执行”按钮,那么在接下来的几秒钟内,我应该能够单击“检查运行”按钮并输出true But it always outputs false because the checkRunning() function returns false . 但是它总是输出false,因为checkRunning()函数返回false
In the script Log though, the isRunning variable is correctly set to true at start and false at the end of execute() . 但是,在脚本Log中, isRunning变量在开始时正确设置为true ,在execute()结束时正确设置为false
Any idea? 任何想法?

My guess is that each client-to-server call always creates new execution context. 我的猜测是,每个客户端到服务器的调用始终会创建新的执行上下文。 In other words, it's not possible to persist variables between function calls, unless you are saving them to a 'stable' object, such as a spreadsheet or a file. 换句话说,除非将变量保存到“稳定”对象(例如电子表格或文件)中,否则不可能在函数调用之间保留变量。 Because google.script.run calls run asynchronously, they get different instances of the global object from .gs file, so the "checkRunning()" function is locked inside its own execution context and has no way to get access to or communicate with parallel threads. 由于google.script.run调用是异步运行的,因此它们会从.gs文件中获取全局对象的不同实例,因此“ checkRunning()”函数被锁定在其自己的执行上下文中,并且无法获取对并行对象的访问或通信线程。

You can work around this limitation by using CacheService to persist data and get rid of global variables altogether. 您可以通过使用CacheService持久化数据并完全摆脱全局变量来解决此限制。 I got this to work by making the following changes 我通过进行以下更改使它起作用

function setIsRunning(value){

  CacheService.getScriptCache().put("isRunning", value.toString());

}

function execute() {

 setIsRunning(true);

  Utilities.sleep(15000); // simulate execution for 15 secs

 setIsRunning(false);

}


function checkRunning() {

var currentState = CacheService.getScriptCache().get("isRunning");

 return (currentState == 'true');
}

Note that I'm returning (currentState == 'true') to convert String to Boolean. 请注意,我要返回(currentState =='true')以将String转换为Boolean。 Boolean('false') will produce true; Boolean('false')将产生true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM