0

Google Drive App Script and Google sheets

I can't get the below script to run without an error. How should I be configuring this script?

  • I'm clearly not understanding what I need to populate / alter / setup in order for the script to run correctly.

If I can get the script to work - the below are my end goals:

  • Is to specify a specific Folder by ID (which needs to be searched)
  • return the data to its bound google sheet to a specific tab as follows: Col1 URL of Folder in question, Col2 Full Folder path ie. FolderName/Subfoldername/sub-subfoldername/etc
  • NOTE: files must be ignored (that would increase the total duration too much and its not required)

The instructions given are as follows: The original instructions given are as follows

function processRootFolder(rootFolder) {
  var MAX_RUNNING_TIME_MS = 1 * 60 * 1000;
  var RECURSIVE_ITERATOR_KEY = "RECURSIVE_ITERATOR_KEY";

  var startTime = (new Date()).getTime();

  // [{folderName: String, fileIteratorContinuationToken: String?, folderIteratorContinuationToken: String}]
  var recursiveIterator = JSON.parse(PropertiesService.getDocumentProperties().getProperty(RECURSIVE_ITERATOR_KEY));
  if (recursiveIterator !== null) {
    // verify that it's actually for the same folder
    if (rootFolder.getName() !== recursiveIterator[0].folderName) {
      console.warn("Looks like this is a new folder. Clearing out the old iterator.");
      recursiveIterator = null;
    } else {
      console.info("Resuming session.");
    }
  }
  if (recursiveIterator === null) {
    console.info("Starting new session.");
    recursiveIterator = [];
    recursiveIterator.push(makeIterationFromFolder(rootFolder));
  }

  while (recursiveIterator.length > 0) {
    recursiveIterator = nextIteration(recursiveIterator, startTime);

    var currTime = (new Date()).getTime();
    var elapsedTimeInMS = currTime - startTime;
    var timeLimitExceeded = elapsedTimeInMS >= MAX_RUNNING_TIME_MS;
    if (timeLimitExceeded) {
      PropertiesService.getDocumentProperties().setProperty(RECURSIVE_ITERATOR_KEY, JSON.stringify(recursiveIterator));
      console.info("Stopping loop after '%d' milliseconds. Please continue running.", elapsedTimeInMS);
      return;
    }
  }

  console.info("Done running");
  PropertiesService.getDocumentProperties().deleteProperty(RECURSIVE_ITERATOR_KEY);
}

// process the next file or folder
function nextIteration(recursiveIterator) {
  var currentIteration = recursiveIterator[recursiveIterator.length-1];
  if (currentIteration.fileIteratorContinuationToken !== null) {
    var fileIterator = DriveApp.continueFileIterator(currentIteration.fileIteratorContinuationToken);
    if (fileIterator.hasNext()) {
      // process the next file
      var path = recursiveIterator.map(function(iteration) { return iteration.folderName; }).join("/");
      processFile(fileIterator.next(), path);
      currentIteration.fileIteratorContinuationToken = fileIterator.getContinuationToken();
      recursiveIterator[recursiveIterator.length-1] = currentIteration;
      return recursiveIterator;
    } else {
      // done processing files
      currentIteration.fileIteratorContinuationToken = null;
      recursiveIterator[recursiveIterator.length-1] = currentIteration;
      return recursiveIterator;
    }
  }

  if (currentIteration.folderIteratorContinuationToken !== null) {
    var folderIterator = DriveApp.continueFolderIterator(currentIteration.folderIteratorContinuationToken);
    if (folderIterator.hasNext()) {
      // process the next folder
      var folder = folderIterator.next();
      recursiveIterator[recursiveIterator.length-1].folderIteratorContinuationToken = folderIterator.getContinuationToken();
      recursiveIterator.push(makeIterationFromFolder(folder));
      return recursiveIterator;
    } else {
      // done processing subfolders
      recursiveIterator.pop();
      return recursiveIterator;
    }
  }

  throw "should never get here";
}

function makeIterationFromFolder(folder) {
  return {
    folderName: folder.getName(), 
    fileIteratorContinuationToken: folder.getFiles().getContinuationToken(),
    folderIteratorContinuationToken: folder.getFolders().getContinuationToken()
  };
}

function processFile(file, path) {
  console.log(path + "/" + file.getName());
}

Kindly note the original message is from [[https://stackoverflow.com/questions/45689629/how-to-use-continuationtoken-with-recursive-folder-iterator]]

I'm trying to use the answer given by Senseful user:35690

I've tried passing in a folder name and a folder ID - instead of the current "rootFolder" in the "function processRootFolder"

I think my issues lay here:

// [{folderName: String, fileIteratorContinuationToken: String?, folderIteratorContinuationToken: String}]
 

However I don't know how to get the "fileIteratorContinuationToken:" for a specific folder or the "folderIteratorContinuationToken:"

I'm hoping that if I can get the basics principle of this code to work. I can them modify the out put to return the info I need into the bound google sheet on a specific tab.

(If I new how to contact the user who wrote the code directly - that too would be of great help)

Thank you,

  • Please create a [mcve] that we can run to reproduce the problem. – Cooper Nov 22 '22 at 17:01
  • Why do you need a recursion iterator? And how do you know you are starting at the same recursion level? How are you reading the stack? – Cooper Nov 22 '22 at 17:23
  • Hi Cooper, I'm using google drive as a "make shift" document management system. I only need to see the folders and their hyper links. From there it is pulled into Data Studio/ Looker Studio in a manner non-IT minded people, can easily search for items, using their phones or laptops. – Jeffery Price Nov 23 '22 at 06:15
  • We're using Google Workspace so the time limit of 30mins has been good enough in the past but we have now exceeded this with the current script I'm running. So, instead if dealing with the timeout of google. I'd rather set the script to run every morning at 1am - it will be done by the time we open. And if the data is 24hours behind it wont be a problem for us. I hope this explains your question. – Jeffery Price Nov 23 '22 at 06:17

0 Answers0