我正在尝试使用“工作示例(线性迭代器)”来实现一个简单的延续令牌这个帖子 https://stackoverflow.com/questions/45689629/how-to-use-continuationtoken-with-recursive-folder-iterator,但我不断收到“异常:无效参数:continuationToken”错误,不知道如何解决此问题。
我尝试将 var userProperties 和 var continuationToken 从全局移动到我的 deleteFile() 内,但仍然不行。调试器还显示 userProperties 填充了数据,但 continuationToken 始终为 null(就在错误之前)。也不知道为什么它总是跳到 else 情况而失败,即使是在第一次运行时也是如此。
const sheetID = "IDgoeshere"; //sheet with ids and user names
const sheetName = 'Sheet1' //name of the sheet in the doc
const emailID = Session.getActiveUser().getEmail();
var userProperties = PropertiesService.getUserProperties();
var continuationToken = userProperties.getProperty('CONTINUATION_TOKEN');
function deleteFile() {
const spreadsheet = SpreadsheetApp.openById(sheetID);
const sheet = spreadsheet.getSheetByName(sheetName);
const lastRow = sheet.getLastRow();
const values = sheet.getRange(2, 4, lastRow - 1, 3).getValues();
values.forEach((row, index) => {
const owner = row[0];
const id = row[2];
if (continuationToken == null) {
if (emailID == row[1]) {
try {
var file = DriveApp.getFileById(row[2])
if (file.isTrashed == true) {
console.info('was trashed');
row++;
}
else {
file.setTrashed(true);
}
}
catch (e) {
console.info(`Unable to find file with id ${id}`);
}
}
else {
// not the first time, pick up where we left off
var files = DriveApp.continueFileIterator(continuationToken); ***//errors out here***
}
}
while (files.hasNext() && end.getTime() - start.getTime() <= maxTime) {
var file = files.next();
Logger.log(file.getName());
end = new Date();
}
if (files.hasNext()) {
var continuationToken = files.getContinuationToken();
userProperties.setProperty('CONTINUATION_TOKEN', continuationToken);
}
else {
// Delete the token
PropertiesService.getUserProperties().deleteProperty('CONTINUATION_TOKEN');
}
});
}
这是一个人为的例子:
function myfunk1(token) {
const folder = DriveApp.getFolderById(gobj.globals.testfolderid);
let files;
if(token == null) {
files = folder.getFiles();
} else {
Logger.log('get continuaton token')
files = DriveApp.continueFileIterator(token);
}
while(files.hasNext) {
let file = files.next();
Logger.log(file.getName());
if(!token ) {
try {
myfunk1(files.getContinuationToken())
}
catch{
return;
}
}
}
}
在此示例中,我只是中断了迭代器的处理,并使用延续标记递归地调用该函数一次。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)