我想知道是否可以循环浏览包含其他文件夹的文件夹,而其他文件夹包含其他文件夹,等等。我不知道有多少个子文件夹有子文件夹,并且想找到一种方法来进行硬编码检查。向下轻松两个级别是足够容易的,但是在那之后,我陷入了如何处理多分支并以以后可以操纵它的方式存储它的麻烦。有小费吗?最后,文件夹结构将用于创建动态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>");
}
}
}
最佳答案
巧合的是,我遇到了类似的问题。我正在收集下级文件夹和文件的文件夹名称和编号。这可能会有所帮助。
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人,因此欢迎您对递归方法进行改进