使用不同的连续令牌构建的迭代器在Google Apps中产生相同的结果 [英] Iterators built with different continuation tokens are producing the same results in Google Apps
问题描述
我正在电子表格中编写Google Apps脚本。我的用例包括遍历一组特定子文件夹的大集合。问题在于处理时间超过了Google允许的最长时间(6分钟),所以我必须编写脚本才能在稍后恢复。我创建了一个触发器来恢复任务,但这不是我问题的一部分(至少,现在不是更重要的)。
我的代码看起来像像这样(减少到最低限度来说明我的问题): 我遇到的问题是迭代器总是返回相同的文件夹集合,尽管它是由不同的标记构成的(我打印了它们,所以我知道它们是不同)。 任何关于我在做什么错的想法? 预先感谢您。 这是由于GAS中的一个错误引起的: I am programming a Google Apps script within a spreadsheet. My use case includes iterating over a large set of folders that are children of a given one. The problem is that the processing takes longer than the maximum that Google allows (6 minutes), so I had to program my script to be able to resume later. I am creating a trigger to resume the task, but that is not part of my problem (at least, not the more important one at this moment). My code looks like this (reduced to the minimum to illustrate my problem): When launchProcess is invoked, it only prepares the program for the other method, continueProcess, that iterates over the set of folders. The iterator is obtained by using the continuation token, when it is present (it will not be there in the first invocation). When the time limit is near, continueProcess obtains the continuation token, saves it in a property and waits for the next invocation. The problem I have is that the iterator is always returning the same set of folders although it has been built from different tokens (I have printed them, so I know they are different). Any idea about what am I doing wrong? Thank you in advance. This is caused by a bug in GAS:
https://code.google.com/p/google-apps-script-issues/issues/detail?id=4116 这篇关于使用不同的连续令牌构建的迭代器在Google Apps中产生相同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
pre $ function launchProcess(){
var scriptProperties = PropertiesService .getScriptProperties();
scriptProperties.setProperty(SOURCE_PARENT_FOLDER_KEY,SOURCE_PARENT_FOLDER_ID);
scriptProperties.deleteProperty(CONTINUATION_TOKEN_KEY);
continueProcess();
}
函数continueProcess(){
try {
var startTime =(new Date())。getTime();
var scriptProperties = PropertiesService.getScriptProperties();
var srcParentFolderId = scriptProperties.getProperty(SOURCE_PARENT_FOLDER_KEY);
var continuationToken = scriptProperties.getProperty(CONTINUATION_TOKEN_KEY);
var iterator = continuationToken == null? DriveApp.getFolderById(srcParentFolderId).getFolders():DriveApp.continueFolderIterator(continuationToken);
var timeLimitIsNear = false;
var currTime;
while(iterator.hasNext()&&!timeLimitIsNear){
var folder = iterator.next();
processFolder_(文件夹);
currTime =(new Date())。getTime();
timeLimitIsNear =(currTime - startTime> = MAX_RUNNING_TIME);
}
if(!iterator.hasNext()){
scriptProperties.deleteProperty(CONTINUATION_TOKEN_KEY);
} else {
var contToken = iterator.getContinuationToken();
scriptProperties.setProperty(CONTINUATION_TOKEN_KEY,contToken);
}
} catch(e){
//发送错误
}
}
$ c $当调用launchProcess时,它只准备其他方法的程序continueProcess,该方法迭代该组文件夹。迭代器是通过使用连续标记获得的(当它存在时(它不会在第一次调用时))。当时间限制接近时,continueProcess获取连续令牌,将其保存在一个属性中并等待下一次调用。
https://code.google.com/p/google-apps-script-issues/issues/detail?id=4116 < a> function launchProcess() {
var scriptProperties = PropertiesService.getScriptProperties();
scriptProperties.setProperty(SOURCE_PARENT_FOLDER_KEY, SOURCE_PARENT_FOLDER_ID);
scriptProperties.deleteProperty(CONTINUATION_TOKEN_KEY);
continueProcess();
}
function continueProcess() {
try {
var startTime = (new Date()).getTime();
var scriptProperties = PropertiesService.getScriptProperties();
var srcParentFolderId = scriptProperties.getProperty(SOURCE_PARENT_FOLDER_KEY);
var continuationToken = scriptProperties.getProperty(CONTINUATION_TOKEN_KEY);
var iterator = continuationToken == null ? DriveApp.getFolderById(srcParentFolderId).getFolders() : DriveApp.continueFolderIterator(continuationToken);
var timeLimitIsNear = false;
var currTime;
while (iterator.hasNext() && !timeLimitIsNear) {
var folder = iterator.next();
processFolder_(folder);
currTime = (new Date()).getTime();
timeLimitIsNear = (currTime - startTime >= MAX_RUNNING_TIME);
}
if (!iterator.hasNext()) {
scriptProperties.deleteProperty(CONTINUATION_TOKEN_KEY);
} else {
var contToken = iterator.getContinuationToken();
scriptProperties.setProperty(CONTINUATION_TOKEN_KEY, contToken);
}
} catch (e) {
//sends a mail with the error
}
}