📅  最后修改于: 2022-03-11 14:59:10.007000             🧑  作者: Mango
function getOrCreateSubFolder(childFolderName, parentFolderName) {
var parentFolder, parentFolders;
var childFolder, childFolders;
// Gets FolderIterator for parentFolder
parentFolders = DriveApp.getFoldersByName(parentFolderName);
/* Checks if FolderIterator has Folders with given name
Assuming there's only a parentFolder with given name... */
while (parentFolders.hasNext()) {
parentFolder = parentFolders.next();
}
// If parentFolder is not defined it sets it to root folder
if (!parentFolder) { parentFolder = DriveApp.getRootFolder(); }
// Gets FolderIterator for childFolder
childFolders = parentFolder.getFoldersByName(childFolderName);
/* Checks if FolderIterator has Folders with given name
Assuming there's only a childFolder with given name... */
while (childFolders.hasNext()) {
childFolder = childFolders.next();
}
// If childFolder is not defined it creates it inside the parentFolder
if (!childFolder) { parentFolder.createFolder(childFolderName); }
return childFolder;
}
function test() {
// Call it looking up in Drive root folder
var a = getOrCreateSubFolder('CHILD');
// Call it looking up in given parent folder
var b = getOrCreateSubFolder('CHILD', 'PARENT');
}