How to list all files (or only certains type of files) inside a main folder and all its subfolders in Google Drive using GAS?

后端 未结 1 1200
没有蜡笔的小新
没有蜡笔的小新 2020-12-20 09:20

Is it possible to create a Google Apps Script which would list :

1) all filenames inside a specific main folder (and in each of its related subfolders)

1条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-20 10:02

    Since it appears you have (really) a lot of files and folders in your drive, I guess the first thing to do would be to know all the folders and subfolders you have in your drive, the script below will create a second sheet in your spreadsheet and show all the folders and their respective trees + urls.

    It shows also the total execution time in seconds, I don't have to remind you that this value should not exceed +-300 seconds...

    So I suggest you try it and report the resulting time here before we can go further.

    function listFolders(){
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      if(!ss.getSheetByName('Sheet2')){ss.insertSheet(1).setName('Sheet2')};
      var sh = ss.getSheetByName('Sheet2');
      var start = new Date();
      var dateString = Utilities.formatDate(new Date(),Session.getTimeZone(), 'MMM-dd-yyyy');
      var topFolder = DocsList.getRootFolder() ; // start point
      var foldersArray = [];
      foldersArray = getFolders(topFolder.getName().replace('Root','MyDrive'),topFolder,foldersArray);
      foldersArray.unshift(['Folders url','path','# SubFolders']);
      //    Logger.log(foldersArray)
      var l = foldersArray.length
      var duration = (new Date().getTime()-start.getTime())/1000;
      var durationString = Utilities.formatString("%01.1f", duration)
      sh.clear();
      sh.setColumnWidth(2,500);
      sh.getRange(1,1,1,foldersArray[0].length).setBackground('#ffffaa').setBorder(true,true,true,true,true,true).setFontWeight('bold');
      sh.getRange(1,1,l,foldersArray[0].length).setValues(foldersArray).setVerticalAlignment('middle').setWrap(false);  
      sh.getRange(l+1,2,1,foldersArray[0].length-1)
      sh.getRange(l+1,1).setFontColor('grey').setFontSize(9).setVerticalAlignment('middle').setWrap(true).setHorizontalAlignment('center').setValue('execution time: '+durationString+' Seconds');
      sh.setFrozenRows(1);
    }
    
    function getFolders(path, container,arrayin) {
      var folders = container.getFolders(0, 500);
      var folderCount = folders.length;
      if(path=='MyDrive'){arrayin.push(['https://drive.google.com/?hl=fr&tab=wo#my-drive',path,folderCount])}
      else{arrayin.push([container.getUrl(),path,folderCount])}
    
      for (var i=0;i

    0 讨论(0)
提交回复
热议问题