2
\$\begingroup\$

I have the recursive function below to cache all the folders of my company Drive into a file. It works like a charm but takes too long, specifically >30 secs to read and save the 300 folders.

What I need is to make it faster somehow. I don't know if it is possible to make the folder reading asynchronous, or if it possible to make a webhook or something to listen to changes in the Drive's folders, which would eliminate the need to run recursively over all folders.

function generateFolderTree( idParent, tree ){
  var currParent = DriveApp.getFolderById( idParent),
      folderChildren = currParent.getFolders(),
      currChild,
      currChildId;

  if( tree.length == 0 ){
    tree.push({ 'text' : currParent.getName(), 'parent' : '#', 'id' : idParent, 'state' : { 'opened' : true }, 'li_attr':{'title': ''} });
  }

  while( folderChildren.hasNext() ){
    currChild = folderChildren.next();
    currChildId = currChild.getId();

    tree.push({ 'text' : currChild.getName(), 'parent' : idParent, 'id' : currChildId, 'li_attr':{'title': currChild.getDescription()} });

    generateFolderTree( currChildId, tree );
  }
}

function geneteAndSaveTheTree(){
  var theTree = [];

  generateFolderTree(folderToIterateId, theTree);

  DriveApp.getFileById(theFileWhereAllFoldersAreStored).setContent(JSON.stringify(theTree));
  return theTree;
}

I know you can get all folders at once for a Drive, but I think it would take even longer to find out all the folders that are child of the folderToIterateId.

\$\endgroup\$
2
\$\begingroup\$

Other than the fact that it's hard to read because your variables are not in Englishref, your code appears solid. Two observations:

  • Some comments would be beneficial, even if only function, parameter and return descriptions. Use of recursion should be clearly explained in comments, for the next poor soul who needs to figure out why they ran out of memory.

  • You have not explicitly handled the case of an invalid folder ID parameter in gerarJqTree().

The only optimization I'd suggest would be to do what you think would take longer; get all folders at once - but do it with the advanced Drive service so you have the container information as well. While you'll need to traverse the returned items to construct the output, you will eliminate several hundred service calls.

\$\endgroup\$
1
  • \$\begingroup\$ I haven't thought on the Advanced Drive, it is indeed alot faster, the same amount of time to retrieve a single folder it in DriveApp it's possible to receive all the folders. I'm still figuring the logic to build the array, since it must be in order from parent to children for the plugin to work, still I think it's going to work, will update OP when finished. \$\endgroup\$
    – Kriggs
    Oct 19 '15 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.