我想使用谷歌应用程序脚本遍历Google云端硬盘中文件夹的树状结构。下面的代码列出了一些文件夹,但不是全部。你能建议什么是深入研究文件夹结构的最佳技术吗?我首先尝试记录所有文件夹名称。
谢谢。
function test() {
listSubfolders(DriveApp.getFolderById('FOLDER_ID'));
}
function listSubfolders(parentFolder) {
var childFolders = parentFolder.getFolders();
while(childFolders.hasNext()) {
Logger.log(childFolders.next().getName());
listSubfolders(childFolders.next());
}
}
答案 0 :(得分:7)
要访问子文件夹,您可以这样做,
function getSubFolders(parent) {
parent = parent.getId();
var childFolder = DriveApp.getFolderById(parent).getFolders();
while(childFolder.hasNext()) {
var child = childFolder.next();
Logger.log(child.getName());
getSubFolders(child);
}
return;
}
function listFolders() {
var parentFolder = DriveApp.getFolderById("0B1n6YLYwFmK_dUpzRWhDRXNwdWc");
var childFolders = parentFolder.getFolders();
while(childFolders.hasNext()) {
var child = childFolders.next();
Logger.log(child.getName());
getSubFolders(child);
}
}
答案 1 :(得分:1)
这应该可以解决问题:
function start() {
folder = DriveApp.getFolderById("0B_5HSTQXtXmsOHBzcnc2dTlkRFU")
listFolders(folder)
}
function listFolders(folder) {
//starting point, we come here from start() or from the bottom of this function
//check the name
var name = folder.getName();
if (name.match("TITL")) {
folder.addEditor("somePerson@gmail.com");
}
//now see if this folder has subfolders
var subfolders = folder.getFolders();
//if it has, we call this function again
while (subfolders.hasNext()) {
listFolders(subfolders.next());
}
}
答案 2 :(得分:1)
此Google脚本将生成包含所有文件和文件夹的Google云端硬盘树。
将此脚本复制到脚本编辑器。从“选择功能”菜单中选择功能“ genFolderTree”。将根文件夹名称“ RootDir”更改为函数genFolderTree()中的根目录
var foldername = 'RootDir';
Google脚本源代码-
function genFolderTree() {
try {
var foldername = 'RootDir';
var folderlisting = 'TreeView_' + foldername;
var parentFolder = DriveApp.getFoldersByName(foldername).next();
var ss = SpreadsheetApp.create(folderlisting);
var sheet = ss.getActiveSheet();
var frontCell = [];
sheet.appendRow([foldername]).getCurrentCell().setFontWeight('bold').setFontColor('red');
frontCell.push(" ");
getChildNode(parentFolder,sheet,frontCell);
var files = parentFolder.getFiles();
while (files.hasNext()) {
frontCell.push(files.next().getName());
sheet.appendRow( frontCell);
frontCell.pop();
}
} catch (e) {
Logger.log(e.toString());
}
}
function getChildNode(parent,sheet,frontCell) {
var childFolders = parent.getFolders();
while (childFolders.hasNext()) {
var childFolder = childFolders.next();
frontCell.push(childFolder.getName())
sheet.appendRow(frontCell);
sheet.getRange(sheet.getLastRow(), frontCell.length).setFontWeight('bold').setFontColor('red');
frontCell.pop();
var files = childFolder.getFiles();
frontCell.push(" ");
var start_row = 0;
var row_no = 0;
while (files.hasNext()) {
frontCell.push(files.next().getName());
sheet.appendRow(frontCell);
if(row_no==0){
start_row = sheet.getLastRow();
}
row_no=row_no+1;
frontCell.pop();
}
if(row_no>0){
var range;
range = sheet.getRange(start_row, frontCell.length,row_no);
// The row grouping depth is increased by row_no.
range.shiftRowGroupDepth(1);
}
// Recursive call for any sub-folders
getChildNode(childFolder,sheet,frontCell);
frontCell.pop();
}
}
您可以从Github link
下载源答案 3 :(得分:0)
认为这种方法可能不那么重复。这会以以下格式输出完整路径名:
'/Root/Directory/Directory/Directory/...'
“Logger”行所在的位置,您可以访问当前文件夹对象(callingFolder),因此此时您可以在该文件夹上插入任何操作。
function myFunction() {
var topFolder = DriveApp.getFolderById("0AMBwhqwkCWBbUk9PVA");
var topPath = "/";
iterateSubFolders(topFolder, topPath);
}
function iterateSubFolders(callingFolder, callingPath) {
var callingFolderName = callingFolder.getName();
var callingFolderFullPath = callingPath + callingFolderName + "/";
Logger.log(callingFolderFullPath);
var childSubFolders = callingFolder.getFolders();
while (childSubFolders.hasNext()) {
var nextSubFolder = childSubFolders.next();
iterateSubFolders(nextSubFolder, callingFolderFullPath);
}
}