0

How can I make my code run faster and avoid timeouts? Most of the time it gives a timeout error, but every once in a while it runs fully. Is there a way to improve on this script, to make it run faster? I am running this on a 1200+ images in folders and subfolders.

// Main function 1: List all folders, & write into the current sheet.
function listFolders(){
  getFolderTree(folderId, false);
};
// Main function 2: List all files & folders, & write into the current sheet.
function listAll(){
  getFolderTree(folderId, true); 
};
// Get Folder Tree
function getFolderTree(folderId, listAll) {
  try {
    // Get folder by id
    var parentFolder = DriveApp.getFolderById(folderId);

    // Initialise the sheet
    var file, data, sheet = SpreadsheetApp.getActiveSheet();
    sheet.clear();
    sheet.appendRow(["Path", "FileName", "Link", "Width", "Height"]);

    // Get files and folders
    getChildFolders(parentFolder.getName(), parentFolder, data, sheet, listAll);

  } catch (e) {
    Logger.log(e.toString());
  }
};
// Get the list of files and folders and their metadata in recursive mode
function getChildFolders(parentName, parent, data, sheet, listAll) {
  var childFolders = parent.getFolders();
  // List folders inside the folder
  while (childFolders.hasNext()) {
    var childFolder = childFolders.next();
    data = [ 
      parentName + " / " + 
      childFolder.getName(),
      // childFolder.getId()
    ];
    // Write
    // sheet.appendRow(data);
    // List files inside the folder
    var files = childFolder.getFiles();
    while (listAll & files.hasNext()) {
      var childFile = files.next();
      let name = childFile.getName();
      let link = childFile.getUrl();
      let fileId = childFile.getId();
      let dims = Drive.Files.get(fileId, {supportsAllDrives: true}).imageMediaMetadata
      let width = dims.width;
      let height = dims.height;
      data = [
        parentName + " / " +  
        childFolder.getName(), 
        name, 
        link, 
        width, 
        height 
        ];     
      sheet.appendRow(data); // Write
    }
    getChildFolders(parentName + " / " + childFolder.getName(), childFolder, data, sheet, listAll);  // Recursive call of the subfolder
  }
};
Adrian
  • 3
  • 3
  • 1
    You could do it with a recursive function. – Cooper Jul 14 '21 at 22:47
  • If it's a timeout error, the only thing you can do is save your progress and set a trigger to run again. What's your timeout limit, is it 6 minutes or 30 minutes? You start a timer, a minute before the time runs out, you stop the script, save your progress to a GSheet or JSON file or whatever, set a trigger to run the same function in 1 minute and when it does it needs to read the data you saved so it can pick up where it left. – Dmitry Kostyuk Jul 14 '21 at 23:21
  • Does this answer your question? [Google app script timeout ~ 5 minutes?](https://stackoverflow.com/questions/14450819/google-app-script-timeout-5-minutes) – Kessy Jul 15 '21 at 07:07
  • @Cooper could you help me figure it out how to do that, I am a newbie and this is overwhelming – Adrian Jul 16 '21 at 05:20
  • https://stackoverflow.com/a/55248127/7215091 – Cooper Jul 16 '21 at 05:25

0 Answers0