【发布时间】:2021-11-24 15:57:48
【问题描述】:
我正在尝试使用来自this post 的“工作示例(线性迭代器)”来实现一个简单的延续令牌,但我不断收到“异常:无效参数:continuationToken”错误,不知道如何解决这个问题.
我尝试将 var userProperties 和 var continuationToken 从全局移动到我的 deleteFile() 中,但仍然不行。调试器还显示 userProperties 填充了数据,但 continuationToken 始终为空(在错误之前)。也不知道为什么它总是跳到 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');
}
});
}
【问题讨论】:
-
前一个迭代器从哪里继续? PropertiesService 不是迭代器。请将您的minimal reproducible example 简化为一个函数,以便我们更轻松地重现问题。您过度使用全局变量使代码难以理解。让我们更轻松,也许您会得到一些帮助。
-
好问题:我不确定。我只是假设工作示例正在运行,并尝试将其应用到我的代码中。似乎它还没有完全工作。我应该从“userProperties.setProperty('CONTINUATION_TOKEN', continuationToken);”开始吗?类型行。我以前没有使用过延续令牌,TBH 我的编码也有很多不足之处。我一直在努力的另一个假设是脚本开始时有一个令牌,并将从那里开始。如果没有,并且需要设置一个,我不知道这一点。编辑:知道了,现在改写为 MRE。
-
把它归结为我认为是一个很好的最小可重现示例。感谢您的提醒。
标签: google-apps-script google-sheets