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