如何在独立脚本运行时锁定电子表格

问题描述 投票:0回答:2

我有一个电子表格,通过onEdit触发器调用独立脚本中的函数。独立函数有循环,可以执行几秒钟。如果用户在独立脚本运行时编辑另一行,则第二行中的信息将与循环中第一行中的信息混淆。

独立脚本中的代码是: -

var tasklistID="mytaslistid"
function getTasks() {
    var tasks=Tasks.Tasks.list(tasklistID)
    return tasks
}

电子表格中的代码是: -

function getTasks(){
    TaskManagerScript.getTasks()
}

安装onEdit触发器调用电子表格getTasks函数,该函数又调用独立的getTasks函数

注意:这是我的代码的简化版本。实际版本还会过滤任务以从特定日期提取任务。这涉及循环遍历列表中的任务,这需要时间

因此,我需要一种方法来锁定电子表格,使其不被编辑,直到独立脚本中的函数完成其执行。

谢谢

google-apps-script google-sheets
2个回答
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');

0
投票

接受TheMaster的评论我带来了一个涉及使用空循环的工作。代码现在修改如下。

在保税脚本中: -

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
}

这样做的诀窍

© www.soinside.com 2019 - 2024. All rights reserved.