0

我想知道是否可以循环遍历包含其他文件夹的文件夹,而其他文件夹包含其他文件夹,依此类推。我不知道有多少子文件夹有子文件夹,并且想找到一种无需硬编码即可检查的方法。向下两层很容易,但在那之后我很难处理多分支并以我以后可以操作的方式存储它。有小费吗?最后,文件夹结构将用于创建动态 Google 站点布局。

这是我到目前为止的一个例子:

function myFunction() {
 //Get the folder
 var topFolders = DocsList.getFolder('TestFolder');
 var lvlOneFolders = topFolders.getFolders();
 var num1Folders = lvlOneFolders.length;
 var someArray = [];

 for (var i = 0; i < num1Folders; i++){

 someArray[i] = lvlOneFolders[i].getFolders();
  }

 Logger.log(someArray);

}

问候,

肖恩

function myFunctionTest() {
  var site = SitesApp.getSite('finance-in-motion.com', 'test-site');
  //Get the folder
  var rootFolder = DocsList.getFolder('TestFolder');
  var rootName = rootFolder.getName();
  var files = rootFolder.getFiles();
  var page;

  site.createWebPage(rootName, rootName,"<h1>What evs21</h1>");

  page = site.getChildByName(rootName);

  createFiles(page,files);

  recursTest(rootFolder);

  }

function recursTest (currentFolder){

  var curLength=currentFolder.getFolders().length;

   for(var i = curLength; i > 0; i --){

     var folders = currentFolder.getFolders();
     var parentName = currentFolder.getName();
     var foldername = folders[i-1].getName();
     var files = folders[i-1].getFiles();
     getSiteTest(foldername,parentName,files);

     if(folders[i-1].getFolders().length != 0){
       recursTest(folders[i-1]);
     }
   }


 }

 function getSiteTest(title,parent,files) {

   var site = SitesApp.getSite('finance-in-motion.com', 'test-site');

   var matches = site.search(parent);

   var page = matches[0];

   var child;

   //create the sibling page
   page.createWebPage(title, title,"<h1>What evs21</h1>");

   child = page.getChildByName(title);

   createFiles(child,files);

  }

 function createFiles(page,files){

   var name;

   if (files.length !=0){

      for (i in files){

         name = files[i].getName();
         page.createWebPage(name,name,"<h1>What evs21</h1>"); 

   }

         }


 }
4

2 回答 2

3

巧合的是,我遇到了类似的问题。我正在收集从属文件夹和文件的文件夹名称和数量。这可能会有所帮助。

    function listFolders() {
    var start = new Date();
    var topFolder = DocsList.getFolderById(TOP) ; // start point
    var foldersArray = [];
    foldersArray = getFolders(topFolder.getName(),topFolder,foldersArray);
    // do what you want with array of folder data
    }
    function getFolders(path, container,arrayin) {
    Logger.log(arrayin)

    var folders = container.getFolders(0, 300);
     var folderCount = folders.length;
         var fileCount = container.getFiles().length;
             arrayin.push([container.getId(),path,fileCount,folderCount]);
                 Logger.log(path + " #files= "+fileCount+ " #folders = "+folderCount);
    for (var i=0;i<folders.length;i++) {


    var thisFolder = folders[i].getName();
    var thisPath = path+"/"+thisFolder;



    getFolders(thisPath,folders[i],arrayin)
    }
return arrayin;
}

我不是最棒的 javascript 人,所以欢迎对递归的东西进行改进

于 2013-04-05T20:38:13.080 回答
2

编辑:使用DavidF 更好的答案我编写了这个电子表格绑定脚本,它可以很好地展示结果,其中包含不同文件类型和总计的列。

只是为了说明;-)

Edit2: 我用 url 替换了 ID,因此我可以直接从 SS 转到该文件夹​​并添加执行时间显示 + 一些格式和公式。(注意:container.getUrl() 不适用于根文件夹,这就是为什么我在 IF 条件下用直接 url 替换它的原因)

这是代码:

function onOpen() {
  var menuEntries = [ {name: "Update data", functionName: "listFolders"}
                     ];
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    ss.addMenu("ListFolders",menuEntries);//
}

function listFolders() {
    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','# Spreadsheets','# Docs','# Forms','# Drawings','# Presentations','# others',])
//    Logger.log(foldersArray)
    var l = foldersArray.length
    var duration = (new Date().getTime()-start.getTime())/1000;
    var durationString = Utilities.formatString("%01.1f", duration)
    var sh = SpreadsheetApp.getActiveSheet().clear();
    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)
    .setFormulas([['="Situation on '+dateString+' ..............Total = "&SUM(C'+(l+1)+':I'+(l+1)+')','=SUM(C2:C'+l+')','=SUM(D2:D'+l+')','=SUM(E2:E'+l+')','=SUM(F2:F'+l+')','=SUM(G2:G'+l+')','=SUM(H2:H'+l+')','=SUM(I2:I'+l+')']])  
    .setBackground('#ffffaa').setBorder(true,true,true,true,true,true).setFontWeight('bold').setVerticalAlignment('middle').setHorizontalAlignment('right');  
    sh.getRange(l+1,1).setFontColor('grey').setFontSize(9).setVerticalAlignment('middle').setHorizontalAlignment('center').setValue('execution time: '+durationString+' Seconds');
    sh.setFrozenRows(1);
}

function getFolders(path, container,arrayin) {
     var folders = container.getFolders(0, 300);
     var folderCount = folders.length;
         var ssCount = container.getFilesByType(DocsList.FileType.SPREADSHEET).length;
         var docCount = container.getFilesByType(DocsList.FileType.DOCUMENT).length;
         var formCount = container.getFilesByType(DocsList.FileType.FORM).length;
         var drawCount = container.getFilesByType(DocsList.FileType.DRAWING).length;
         var presCount = container.getFilesByType(DocsList.FileType.PRESENTATION).length;
         var otherCount = container.getFilesByType(DocsList.FileType.OTHER).length;

         if(path=='MyDrive'){arrayin.push(['https://drive.google.com/?hl=fr&tab=wo#my-drive',path,folderCount,ssCount,docCount,formCount,drawCount,presCount,otherCount])}
         else{arrayin.push([container.getUrl(),path,folderCount,ssCount,docCount,formCount,drawCount,presCount,otherCount])}

    for (var i=0;i<folders.length;i++) {
      var thisFolder = folders[i].getName();
      var thisPath = path+"/"+thisFolder;
      getFolders(thisPath,folders[i],arrayin)
      }
  return arrayin;
}

在此处输入图像描述


上一个答案:

这是一条路。

它从根开始扫描 3 个级别,但您可以通过遵循相同的结构轻松增强它以使其更深入。

结果出现在一个数组中,所有文件夹都以经典方式(文件夹/子文件夹/子子文件夹)表示,因为我不知道您想如何使用它。 3

function folderTree() {
 var result = [] 
 var foldersL1 = DocsList.getRootFolder().getFolders()
   for(n=0;n<foldersL1.length;++n){
result.push(foldersL1[n].getName())
     var foldersL2 = foldersL1[n].getFolders()
       for(m=0;m<foldersL2.length;++m){
result.push(foldersL1[n].getName()+'/'+foldersL2[m].getName())       
         var foldersL3 = foldersL2[m].getFolders()
            for(o=0;o<foldersL3.length;++o){
result.push(foldersL1[n].getName()+'/'+foldersL2[m].getName()+'/'+foldersL3[o].getName()+'/')
            }
          }
        }
Logger.log(result)     
}
于 2013-04-04T21:26:47.610 回答