在Google表格中代码在后台运行时发出警报 [英] Alert when code is running background in google sheet

查看:108
本文介绍了在Google表格中代码在后台运行时发出警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Google App脚本和Javascript,我设法检索了xls Gmail附件,将其转换为google工作表,向某些收件人发送警告电子邮件,并在向其中添加新附件时插入带有Google云端硬盘文件夹链接的事件,最后我用一个独立的脚本进行管理,以将来自xls文件的某些数据导入转换为主Google表格的位置,其中一位操作员正在使用h24(更新一些数据,以便稍后在服务期间或在服务本身期间转发). 独立脚本每30分钟运行一次,以检查收到的邮件(第二天或第二天的计划),并在找到后执行代码. 到目前为止,锁定服务在独立脚本上尚不可用,因此我无法锁定文档,因此我希望向用户显示一条消息,该脚本即将运行,并且不进行任何编辑,因为绑定到电子表格的其他脚本是使用锁定服务,因此结果可能会令人非常失望.我搜索了有关此警报"的信息,但找不到有关它的线索. 你能在某个地方给我讲话吗? 我可以添加菜单功能而不是独立脚本,操作员可以选择导入数据",仅此而已,但是我希望在后台自动进行操作.

Working with Google App script and Javascript, I managed to retrieve xls Gmail attachments, convert it to google sheets, send an warning email to some recipients, insert an event with the Google Drive folder link when a new attachment is added to it, and at the end I managed with a standalone script to import some of the data coming from such xls file converted to a main Google Sheet where one operator is working h24 ( updating some data to be forwarded later on and during the service itself). The standalone script run every 30 minutes to check incoming mail ( the schedule for the next day or days), and when it find it, it execute the code. The lock service is not available on standalone scripts up to now, so I cannot lock the document, so I wish to display a message to the user that the script is about to run and not edit anything, since other scripts bounded to the Spreadsheet are using the Lock service, and so the result could be quite disappointing. I searched info regarding this "alert", but I cannot find a clue about it. Could you address me somewhere? I could add a menu function instead of a standalone script, and the operator could select "Import data" and that's it, but I wish to automate the operation in background.

推荐答案

您可以使用HTML服务.我本人对此并不了解,但我认为以下可能会满足您的需求:

You could use the HTML Service. I don't know much about it myself but I think the following may do what you need:

.gs文件顶部:

var ss = SpreadsheetApp.getActiveSpreadsheet();
var htmlApp = HtmlService.createTemplateFromFile("html");     
var status = "Loading....."
 htmlApp.data = status;
 ss.show(htmlApp.evaluate()
 .setWidth(300)
 .setHeight(200));

最后(紧接大括号之前):

At the end (just before the last brace):

var status = "Finished!"
 htmlApp.data = status;
 ss.show(htmlApp.evaluate()
 .setWidth(300)
 .setHeight(200));

在名为"html"的新HTML脚本文件中:

In a new HTML script file named 'html':

<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<h2>Updating..</h2>
<p id="status">(innerHTML).</p>
<div id="imageico"></div>
<script>
var imageContainer = document.getElementById("imageico");
if (<?= data ?> != "Finished!"){
document.getElementById("status").innerHTML = "Running script, please wait..";
} else {
document.getElementById("status").innerHTML = "Finished! You can now close the window.";
closeBtn();

}  
   function closeBtn(){
   var button = document.createElement("INPUT");
    button.setAttribute("type", "button");
    button.setAttribute("value", "Close");
    button.setAttribute("onclick", "closeWindow();");
    imageContainer.appendChild(button);
    }

function closeWindow(){
google.script.host.close();
}
</script>
</body>
</html>

已编辑基于评论

在您的.gs文件中:

 function test() {

    htmlApp("","");

/*
*
*  Put your code in here....
*
*/     

  Utilities.sleep(3000); // change this value to show the "Running script, please wait.." HTML window for longer time.

  htmlApp("Finished!",""); 

  Utilities.sleep(3000);  // change this value to show the "Finished! This window will close automatically. HTML window for longer time.

  htmlApp("","close"); // Automatically closes the HTML window.

}

 function htmlApp (status,close) {
     var ss = SpreadsheetApp.getActiveSpreadsheet();
     var htmlApp = HtmlService.createTemplateFromFile("html");     
      htmlApp.data = status;
      htmlApp.close = close;
      ss.show(htmlApp.evaluate()
     .setWidth(300)
     .setHeight(200));
}

在名为"html"的HTML脚本文件中:

In the HTML script file named 'html':

<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<h2>Updating..</h2>
<p id="status">(innerHTML).</p>
<div id="imageico"></div>
<script>
var imageContainer = document.getElementById("imageico");
if (<?= data ?> != "Finished!"){
document.getElementById("status").innerHTML = "Running script, please wait..";
} else {
document.getElementById("status").innerHTML = "Finished! This window will close automatically.";
}     
if (<?= close ?> == "close"){
google.script.host.close();
}
</script>
</body>
</html>

看看这个示例电子表格

这篇关于在Google表格中代码在后台运行时发出警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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