我有一个Google脚本,列出了我的Google Drive文件夹的FolderName,FolderID,Filename和FileID(包括子文件夹)。但是,在第一行中,我需要完整的文件完整路径,而不是仅文件夹名
例如,名为abc.pdf的文件位于/documents/files/new/abc.pdf文件夹中
我的脚本向我显示了文件夹名称new而不是完整的文件夹路径/ documents / files / new /
`function listFolderContents() {
var ss = SpreadsheetApp.getActive();
var sheet = SpreadsheetApp.getActiveSheet();
sheet.clearContents();
sheet.appendRow(["Foldername","FolderID","Filename","FileID"]);
var topFolderID = DriveApp.getFolderById('FOLDER_ID_HERE');
var foldername = topFolderID.getName();
var folders = DriveApp.getFoldersByName(foldername)
var folder = folders.next();
traverseFolder(folder, sheet);
sortByFoldername();
};
function traverseFolder(folder, sheet) {
listFilesInFolder(folder, sheet);
var subFolders = folder.getFolders();
while (subFolders.hasNext()) {
traverseFolder(subFolders.next(), sheet);
}
}
function listFilesInFolder(folder, sheet) {
var folderID = folder.getId();
var foldername = folder.getName();
var contents = folder.getFiles();
while(contents.hasNext()) {
var file = contents.next();
var filename = file.getName();
var fileID = file.getId();
sheet.appendRow( [foldername,folderID,filename,fileID] );
}
}
function sortByFoldername(){
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName('Sheet1');
var range = sheet.getRange("A2:D");
range.sort([{column: 1, ascending: true}]);
}`
我终于做到了!!!此脚本将按预期工作,并将创建一个包含Folder和文件名的列表,其中包含Google Drive Fiolder中包含的所有文件夹和文件:
var folderId = 'FOLDERIDGOESHERE'; //Replace the folder id with the folder id of the parent folder in the Google Drive, where you would like to start sorting through the drive.
// Main function 1: List all folders, & write into the current sheet.
function listFolders(){
getFolderTree(folderId, false);
};
// Main function 2: List all files & folders, & write into the current sheet.
function listAll(){
getFolderTree(folderId, true);
};
// =================
// Get Folder Tree
function getFolderTree(folderId, listAll) {
try {
// Get folder by id
var parentFolder = DriveApp.getFolderById(folderId);
// Initialise the sheet
var file, data, sheet = SpreadsheetApp.getActiveSheet();
sheet.clear();
sheet.appendRow(["Full Path", "Folder ID", "FileName"]);
// Get files and folders
getChildFolders(parentFolder.getName(), parentFolder, data, sheet, listAll);
} catch (e) {
Logger.log(e.toString());
}
};
// Get the list of files and folders and their metadata in recursive mode
function getChildFolders(parentName, parent, data, sheet, listAll) {
var childFolders = parent.getFolders();
// List folders inside the folder
while (childFolders.hasNext()) {
var childFolder = childFolders.next();
// Logger.log("Folder Name: " + childFolder.getName());
data = [
parentName + "/" + childFolder.getName(),
childFolder.getId()
];
// Write
sheet.appendRow(data);
// List files inside the folder
var files = childFolder.getFiles();
while (listAll & files.hasNext()) {
var childFile = files.next();
// Logger.log("File Name: " + childFile.getName());
data = [
parentName + "/" + childFolder.getName(),
childFolder.getId(),
childFile.getName(),
childFile.getId()
];
// Write
sheet.appendRow(data);
}
// Recursive call of the subfolder
getChildFolders(parentName + "/" + childFolder.getName(), childFolder, data, sheet, listAll);
}
};