我有一个电子表格,可以通过onEdit触发器在独立脚本中调用函数。独立函数具有循环,可以执行几秒钟。如果用户在独立脚本运行时编辑另一行,则循环中第二行的信息会与第一行的信息混淆。
独立脚本中的代码是:-
var tasklistID="mytaslistid"
function getTasks() {
var tasks=Tasks.Tasks.list(tasklistID)
return tasks
}
电子表格中的代码为:-
function getTasks(){
TaskManagerScript.getTasks()
}
安装的onEdit触发器将调用电子表格getTasks函数,而电子表格又将调用独立的getTasks函数
注意:这是我的代码的简化版本。实际版本还会过滤任务以从特定日期提取任务。这涉及遍历列表中的任务,这很费时间
因此,我需要一种方法来锁定电子表格,使其无法编辑,直到独立脚本中的功能完成执行为止。
谢谢
答案 0 :(得分:1)
您可以使用modal
对话框:
模式对话框可防止用户与对话框进行交互。
// Display a modal dialog box with custom HtmlService content and script
var htmlOutput = HtmlService
.createHtmlOutput('<p>Please wait...</p>')
.append('<script>google.script.run.withSuccessHandler(google.script.host.close).callStandaloneFunction()</script>')
.setWidth(250)
.setHeight(300);
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'My add-on');
答案 1 :(得分:0)
根据TheMaster的评论,我进行了一项工作,涉及使用空的while循环。现在修改代码如下。
在绑定脚本中:-
function getTasks(){
\\code to open modal dialog
showDialog()
\\Empty while loop with call to standalone script ==true as condition to end loop
while(TaskManagerScript.addTask(e)==false){}
\\code to close modal dialog
closeDialog()
}
在独立脚本中:-
var tasklistID="mytaslistid"
function getTasks() {
var tasks=Tasks.Tasks.list(tasklistID)
\\some more codes
return true
}
绝招