I want to create some folders (a,b,c) in each subFolder of A (A.1, A.2, A.3) which will reside in folder A and then create multiple GDocs in folder b.
The directory in the Google Drive:
Code:
//get the values from two cols.
var chapters = currSheet.getRange("B2:B").getValues();
var subChapters = currSheet.getRange("C2:C").getValues();
//get the count of Chapter and the SubChapters and calls the getSubFoldersM() for subFolder and GDoc creation.
var count = 1;
var length = subChapters.length;
console.log("Chapter: " + chapters[0]);
var ParentFolder = getFolder.createFolder("Chapter "+chapters[0]);
getSubFoldersM(ParentFolder, subChapters[i] ,count,link);
var lastchaptersValue = chapters[0];
for (i = 0; i < length; i++)
{
if (subChapters[i] === subChapters[i + 1])
{
count++;
continue;
}
else
{
if ( chapters[i] != lastchaptersValue )
{
console.log("Chapter: " + chapters[i]);
var ParentFolder = getFolder.createFolder("Chapter "+chapters[i]);
}
lastchaptersValue = chapters[i];
}
console.log(subChapters[i], count);
getSubFoldersM(ParentFolder, subChapters[i] ,count,link);
count = 1;
}
//function for creation of Subfolders and GDocs Creation
function getSubFoldersM(rootFolder,uniqueChapter,finalCount,rootName){
var subFolder = rootFolder.createFolder("Chapter "+uniqueChapter);
subFolder.createFolder("Spreadsheet Links");
subFolder.createFolder("Images");
var solutionFolder = subFolder.createFolder("Solutions");
var name = subFolder.getName();
for (var i=0;i<finalCount;i++)
{
var doc = DocumentApp.create(rootName+'_'+name+''+columnToLetterM(i+1)+'_EL'),
docFile = DriveApp.getFileById( doc.getId() );
solutionFolder.addFile( docFile );
DriveApp.getRootFolder().removeFile(docFile);
}
}
This is the simple piece of code that gets me my required directory. But it is taking a LOT of time as there is nearly 10k data. Roughly estimated, 9.5k rows of data it took 5 minutes to create all the folders and GDocs. Also, I am using folderIterator()
every time in another for loop which I'm told takes a lot of time.
How to make this code more time-efficient?