Untitled

mail@pastecode.io avatar
unknown
javascript
2 years ago
860 B
1
Indexable
Never
/**
 * @param {any} rootFolder : Root Folder
 * @param {any[]} children : Array of children name
 */
function createFolder(rootFolder, ...children) {
  
  if (!rootFolder) throw new Error('Root folder is null or undefined');

  const create = (root, child) => {
    const children = root.getFoldersByName(child);
    if (children.hasNext()) return children.next();
    return root.createFolder(child);
  }

  let resultFolder = null;
  let tempRoot = rootFolder;

  for (const child of children) {
    resultFolder = create(tempRoot, child);
    tempRoot = resultFolder;
  }
  
  return resultFolder;
}


function test() {
  const root = DriveApp.getFolderById('1QJcgmraWKgAY2_g4a3w_vnjV8fumdUQa');
  const res = createFolder(root, 'Thư mục 1', 'Thư mục 2', 'Thư mục 3', 'Thư mục 4');
  console.log(res.getId());
}