谷歌应用脚本超时 ~ 5 分钟? [英] Google app script timeout ~ 5 minutes?
问题描述
我的 google 应用程序脚本正在遍历用户的 google 驱动器文件,并将文件复制并有时移动到其他文件夹.脚本总是在 5 分钟后停止,日志中没有错误消息.
My google app script is iterating through the user's google drive files and copying and sometimes moving files to other folders. The script is always stopped after 5 minutes with no error message in the log.
我在一次运行中对数十甚至数千个文件进行排序.
I am sorting tens or sometimes thousands files in one run.
是否有任何设置或解决方法?
Are there any settings or workarounds?
推荐答案
你可以做的一件事(这当然取决于你想要完成的事情)是:
One thing you could do (this of course depends on what you are trying to accomplish) is:
- 将必要的信息(例如循环计数器)存储在电子表格或其他永久存储(例如 ScriptProperties)中.
- 让您的脚本每五分钟左右终止一次.
- 设置一个时间驱动触发器以每五分钟运行一次脚本(或使用 脚本服务).
- 每次运行时,从您使用过的永久存储中读取保存的数据,然后从停止的位置继续运行脚本.
这不是一个万能的解决方案,如果您发布代码,人们将能够更好地为您提供帮助.
This is not a one-size-fit-all solution, if you post your code people would be able to better assist you.
这是我每天使用的脚本的简化代码摘录:
Here is a simplified code excerpt from a script that I use every day:
function runMe() {
var startTime= (new Date()).getTime();
//do some work here
var scriptProperties = PropertiesService.getScriptProperties();
var startRow= scriptProperties.getProperty('start_row');
for(var ii = startRow; ii <= size; ii++) {
var currTime = (new Date()).getTime();
if(currTime - startTime >= MAX_RUNNING_TIME) {
scriptProperties.setProperty("start_row", ii);
ScriptApp.newTrigger("runMe")
.timeBased()
.at(new Date(currTime+REASONABLE_TIME_TO_WAIT))
.create();
break;
} else {
doSomeWork();
}
}
//do some more work here
}
注意#1:变量 REASONABLE_TIME_TO_WAIT
应该足够大以触发新触发器.(我将它设置为 5 分钟,但我认为它可能会更短).
NOTE#1: The variable REASONABLE_TIME_TO_WAIT
should be large enough for the new trigger to fire. (I set it to 5 minutes but I think it could be less than that).
注意#2:doSomeWork()
必须是一个执行相对较快的函数(我会说不到 1 分钟).
NOTE#2: doSomeWork()
must be a function that executes relatively quick( I would say less than 1 minute ).
注意#3:Google 已弃用 脚本属性
,并引入了属性服务
代替它.功能做了相应的修改.
NOTE#3 : Google has deprecated Script Properties
, and introduced Properties Service
in its stead. The function has been modified accordingly.
注意#4:第二次调用该函数时,它将for循环的第i个值作为字符串.所以你必须把它转换成整数
NOTE#4: 2nd time when the function is called, it takes the ith value of for loop as a string. so you have to convert it into an integer
这篇关于谷歌应用脚本超时 ~ 5 分钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!