Loading...

Quick folder creation using app script from data present in google sheet

Posted on 15-Jul-2023 11:21 PM

Price - Free

(If you are facing problems during checkout, please Contact us)

Details

Create desired folders and sub folders using google sheets scripting.

Description

Note: You can get access to the complete proejct by filling up the form on the right.

Automatic folder creation in Google Drive using Google Sheets and App Script can greatly enhance productivity and organization. By leveraging this feature, users can save time and effort by automating the creation of folders based on predefined criteria. It enables seamless organization of files, ensuring that documents are stored in the right location from the start. This eliminates the need for manual folder creation and reduces the risk of human error. Furthermore, it promotes consistency across teams and facilitates collaboration by providing a standardized folder structure. With automatic folder creation, users can focus on their work without worrying about file organization, resulting in improved efficiency and streamlined workflows.
 

Here is the code that was used to make it work, please make changes as requried.

var superParentFolderID = getSuperParentFolder();

var superParentFolder =DriveApp.getFolderById(superParentFolderID);


const templateFileId = superParentFolder.getFoldersByName("Template").next().getFilesByName("Sample").next().getId();

var templateFile = DriveApp.getFileById(templateFileId);

var templateFileName = templateFile.getName();


function getSuperParentFolder(){

  ssID = SpreadsheetApp.getActive().getId();

  var file = DriveApp.getFileById(ssID);

  var folders = file.getParents();

  var parentFolderId = folders.next().getId();

  return parentFolderId;

}


function startFolderCreation(){

  var s               = SpreadsheetApp.getActiveSpreadsheet();

  var ss              = s.getSheetByName('Sheet1');

  var folderRange     = ss.getDataRange();

  var folderRangeData = folderRange.getValues();

  var folderChain = "";

  var newFolder;

  var updateValue = "Y";

  for (var i = 1; i < folderRangeData.length; i++) {

    if (folderRangeData[i][0] != '' && folderRangeData[i][2] != 'Y') {

      var folderName = (folderRangeData[i][0]);

      var folderChildName = (folderRangeData[i][1]);

      //if superparent has this folder create inside that

      if (folderExists(folderName, superParentFolder)) {

        //check for child folder

        var pF =DriveApp.getFoldersByName(folderName).next();

        if (folderExists(folderChildName, pF)) {

          updateValue = "N";

        }else{

          newFolder = superParentFolder.getFoldersByName(folderName).next().createFolder(folderChildName);

        }  

      } else {

      //if superparent does not have this folder create folder and then create subfolder inside that

        newFolder = superParentFolder.createFolder(folderName).createFolder(folderChildName);

      }

      var newDocFileId = templateFile.makeCopy(templateFileName, newFolder).getId();

      SpreadsheetApp.getActive().getSheetByName("Sheet1").getRange(i+1,3).setValue(updateValue);

    }

  }

}


//Tells us if a folder name already exsts inside a parent folder

function folderExists(foldername,parentFolder) {

  let folders;

  if(parentFolder) {

    folders = parentFolder.getFoldersByName(foldername);//only looks at immediate sub folder

  } else {

    folders = DriveApp.getFoldersByName(foldername);//looks on entire drive

  }

  let fA = [];

  while (folders.hasNext()) {

    let folder = folders.next();

    fA.push(folder.getId())

  }

  return fA.length > 0 ? true : false;

}

Get Access
@gmail.com
Only Gmail id is accepted.

This is a free item.
Notice Board
📢Free consultation👉 Telegram Channel: sheetsprojects.com
Join My Groups

Connect with fellow members and stay updated on group discussions!

Join my WhatsApp Group

Join our Telegram group and share ideas with like-minded individuals!

Join my Telegram Group

Stay updated on news, events, and more by liking our Facebook Page!

Like my Facebook Page