I'm trying to implement a simple continuation token using the "Working example (linear iterator)" from this post, but I keep getting a "Exception: Invalid argument: continuationToken" error, not sure how to get around this.
I've tried moving the var userProperties and var continuationToken around from global to to within my deleteFile(), but still no go. Debugger also show that userProperties fills with data, but continuationToken is always null(right before the error). Also not sure why it always jumps down to the else case to fail out, even on first run.
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');
}
});
}