问题
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...
}
回答1:
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):
Workaround#1: Use Different projects
- Create a new project: In the editor>File>New>Project
- First project's
onOpen()
will runinfLoop()
- Second project's
onOpen()
will runrunScript()
- Both functions will run simultaneously on open.
Workaround#2: Simple and Installable trigger1
- Create a installable trigger for
runScript()
- Simple trigger
onOpen()
will runinfLoop()
- Both functions will run simultaneously on open.
- You could use two installable triggers instead of simple and installable trigger.
Workaround#3: Web apps: Call from client
- 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)
Workaround#4: Web apps: UrlFetchApp#fetchAll2
- 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.
Workaround#5: onEdit/onChange
- If a edit is made, both functions(onEdit/onChange) run simultaneously.
Workaround#6: 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.
回答2:
To run two or more functions "at the same time" you should call each function separately. One way is to use promises from client side code.
Bear in mind that your server-side infinite loop eventually will cause that your script exceed the maximum execution time (6 minutes / 30 minutes depending on the account type that effective user is using).
Related
- Using promises for mulitple queries to Google sheets
回答3:
Running an infinite loop in Apps Script is futile, since there's an enforced maximum execution time of 6 minutes for most scripts. When you hit that limit the script execution will be killed.
Judging from your example script, what you're attempting to do is a scheduled job to set the document name. For that purpose, you would be better served using a time-driven trigger.
You could then structure your script like this:
function onOpen() {
// code...
}
function updateDocumentName() {
DocumentApp.getActiveDocument().setName("dont change this name")
}
Then, you can setup a time-driven trigger associated with the updateDocumentName()
function.
One major difference to note: instead of executing the logic every 100ms, the highest frequency you can set with a time-driven trigger is once every 1 minute.
回答4:
YES! Simply put one function inside the other.
function EmailSummary() {
// Modified from http://stackoverflow.com/a/22200230/1027723 .. SOURCE: https://mashe.hawksey.info/2015/07/tips-on-emailing-inline-google-charts-from-sheets-using-apps-script/
function emailCharts(sheet_name){ // eg "SF_EMAIL"
sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheet_name)
var charts = sheet.getCharts();
if(charts.length==0){
MailApp.sendEmail({
...
Just make sure to indent correctly.
来源:https://stackoverflow.com/questions/58064351/is-there-a-way-to-run-two-function-at-the-same-time-in-google-apps-script-with-o