有没有一种方法可以在Google Apps脚本中同时运行两个函数,而一个函数是无限循环? [英] Is there a way to run two function at the same time in Google Apps Script with one function as an infinite loop?

查看:129
本文介绍了有没有一种方法可以在Google Apps脚本中同时运行两个函数,而一个函数是无限循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想同时运行两个函数,但是我不能只让它们分别运行,因为一个函数包含一个无限循环while(true). JavaScript的问题在于,如果您要在哪里运行两个函数,它将在运行下一个函数之前完成该函数的运行.因此,如果我使用while(true)循环运行一个函数,它将永远不会移至下一个函数.
如果您仍然不明白,请参阅以下代码:

I have two functions that I want to run at the same time but I can't just let them run separately as one function contains an infinite loop while(true). And the problem with JavaScript is that if you where to run two functions, it will finish running the function before running the next one; so if I run a function with a while(true) loop, it will never move onto the next function.
If you still don't understand, here is my code:

function onOpen(){           // Google Apps Script trigger
    infLoop()                //how to run both of these functions at the same time?
    runScript()
}

function infLoop(){          //inf loop.

    while(True){
        Utilities.sleep(100)
        DocumentApp.getActiveDocument()
        .setname("dont change this name")
    }
}

function runScript(){
    //code...
}

推荐答案

Google Apps脚本同步执行.在大多数情况下,无法同时进行/并行处理.根据您的脚本,似乎您希望两个函数在onOpen上同时运行.可能的解决方法(有些未经测试):

Google apps script executes synchronously. For the most part, simultaneous/paralell processing is not possible. Based on your script, it seems you want two functions to run simultaneously onOpen. Possible workarounds(Some not tested):

  • 创建新项目:在编辑器>文件>新建>项目
  • 第一个项目的onOpen()将运行infLoop()
  • 第二个项目的onOpen()将运行runScript()
  • 这两个功能将同时在打开时运行.
  • Create a new project: In the editor>File>New>Project
  • First project's onOpen() will run infLoop()
  • Second project's onOpen() will run runScript()
  • Both functions will run simultaneously on open.
  • runScript()
  • 创建可安装的触发器
  • 简单触发器onOpen()将运行infLoop()
  • 这两个功能将同时在打开时运行.
  • 您可以使用两个可安装的触发器,而不是简单且可安装的触发器.
  • Create a installable trigger for runScript()
  • Simple trigger onOpen() will run infLoop()
  • Both functions will run simultaneously on open.
  • You could use two installable triggers instead of simple and installable trigger.
  • 如果打开了侧边栏或从Web应用程序打开了工作表,则可以通过google.script.run(异步运行)重复调用服务器功能
  • If there is a sidebar open or if a sheet is opened from web app, it is possible to call server functions repeatedly through google.script.run(which run asynchrously)
  • UrlFetchApp#fetchAll异步运行
  • 发布Web应用程序后,发布的URL可以与查询参数一起使用.如果将函数名称作为参数发送并且doGet()执行该函数,则.fetchAll可以异步用于多个函数.
  • UrlFetchApp#fetchAll runs asynchronously
  • Once a web app is published, the published url can be used with query parameters. If a function name is sent as a parameter and doGet() executes the function, .fetchAll can be used to multiple functions asynchronously.
  • 如果进行编辑,则两个功能(onEdit/onChange)将同时运行.
  • 如果加载项/脚本通过工作表api进行更改,则可能触发onChange .如果被触发,则通过sheets api进行的每个更改都会导致onChange异步运行.
  • If a add-on/script makes a change through sheets api, onChange may get triggered. If triggered, every change made through sheets api causes onChange to run asynchronously.

这篇关于有没有一种方法可以在Google Apps脚本中同时运行两个函数,而一个函数是无限循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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