I am trying to find a specific spreadsheet(Target) within each folder (A). These folders are within Folder (B) which are in turn within Folder (C). The current script i have retrieve folder (C), and search through each (B) for folder (A) then spreadsheet. However, because of large numbers of folder (B), i have placed a continuationtoken at the folder (A) level to track which folder (B) have been searched [or at least i believe that's what i am doing]. The issue I have is that the script always resume from the same first folder (B) instead of continuing from the last folder (B) that was searched.
Folder (C) = baseFolder
Folder (B) = studentFolder
Folder (A) = AssessmentFolder
Spreadsheet (Target) = any spreadsheet which includes the given searchkey
Following are snippets of the script that i am using.
if (continuationToken == null) {
// firt time execution, get all files from Drive
var allFolders = baseFolder.getFolders();
var Tokenalert = ui.alert('There is no token');
}
else {
// not the first time, pick up where we left off
var allFolders = DriveApp.continueFolderIterator(continuationToken);
}
while (allFolders.hasNext() && end.getTime() - start.getTime() <= maxTime) {
i++;
var studentFolder = allFolders.next();
var AssessmentFolder = studentFolder.getFoldersByName("02 Assessment");
if (AssessmentFolder.hasNext() == true){
Logger.log(studentFolder.getName());
progress.getRange(i,1).setValue(studentFolder.getName());
AssessmentFolder = AssessmentFolder.next();
if (AssessmentFolder.searchFiles(checkcode).hasNext() == true){
var filetochange = AssessmentFolder.searchFiles(checkcode);
var editfile = filetochange.next();
progress.getRange(i,2).setValue(editfile);
Logger.log(editfile);var fileid = editfile.getId();
var editss = SpreadsheetApp.openById(fileid);
Logger.log(editss.getName());
progress.getRange(i,3).setValue(fileid);
var editsheet = editss.getSheetByName(sheettoedit);
// remove protection from the sheet mentioned
// Protect the active sheet except B2:C5, then remove all other users from the list of editors.
var protection = editsheet.protect().setDescription('Test');
if (protectrange != 0) {
var unprotected = editsheet.getRange(protectrange);
}
if (protectrange2 != 0) {
var unprotected2 = editsheet.getRange(protectrange2);
}
else {
unprotected2 = unprotected;
}
if (protectrange3 != 0) {
var unprotected3 = editsheet.getRange(protectrange3);
}
else {
unprotected3 = unprotected2;
}
protection.setUnprotectedRanges([unprotected, unprotected2]);
// Ensure the current user is an editor before removing others. Otherwise, if the user's edit
// permission comes from a group, the script throws an exception upon removing the group.
var me = Session.getEffectiveUser();
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
progress.getRange(i,4).setValue("complete");
}
else {
progress.getRange(i,4).setValue("fail");
}
}
end = new Date()
}
// Save your place by setting the token in your user properties
if(allFolders.hasNext()){
var continuationToken = allFolders.getContinuationToken();
userProperties.setProperty('CONTINUATION_TOKEN', continuationToken);
progress.getRange(1,6).setValue(continuationToken);
} else {
i++;
progress.getRange(i,1).setValue("Completed")
// Delete the token
PropertiesService.getUserProperties().deleteProperty('CONTINUATION_TOKEN');
ss.deleteSheet("Progress");
var Completionalert = ui.alert('completed');
}
Pardon my messy code as I am new to coding.
I have checked and the continuation token is stored, and is retrieved. I have also ensured that the script does not end prematurely before the token is stored. The only issue i can think of is that either the way I enter the token again is wrong, or a wrong token is stored. But i am unsure of which. I have tried storing the token at B level and A level, but it doesn't make sense and doesn't work.
Also, I have read the following:
https://developers.google.com/apps-script/reference/drive/folder-iterator Howver it was not very helpful as it only shows how to get a token.
Google Apps Script: How to use ContinuationToken with recursive folder iterator but i do not understand the the term recursive.
It would be great if someone can explain how continuationtoken works. Does it just track the last folder and resume from there? or does it actually take the whole list of folder and gives the position?
To reiterate, the main issue is that the token is created and retrieved, but somehow the script is not resuming from the last folder checked.
Any offhand feedback on other portions of the script is welcomed^^ and thank you in advance!:)