google-apps-scriptgoogle-drive-shared-drive

Is there a way to automatically copy a template into every new google drive folder created?


I'm new to google app scripts and am trying to see if there is a way to automatically create a copy of a google sheets template and google docs template into every new folder created. Currently, there is a template that is needed in every folder for new members and we are manually making a copy and moving it to each new folder. This is all occurring in a shared drive. The goal flow is:

  1. Two new members joins
  2. New google subfolders are manually created (001 John Smith & 002 Jane Doe) within the 'Members' folder
  3. Two templates are automatically copied and moved into the '001 John Smith' and '002 Jane Doe' folders

This should apply to all new folders that are created in the 'Member' folder within the google drive.

Appreciate any and all help on this!

I found the below code but it looks like it is specific to moving a copy to one folder vs. to all new folders (based on the getFolderbyID portion).

//get template 
var SStemplate = "URL here";
var SStocopy = SpreadsheetApp.openByUrl(SStemplate); 

//set destination folder
var SSdestFolder = DriveApp.getFolderById("0B-hjh1DycmoweEl5ZmgxdFBRYzg");

//copy template to destination 
var newFile = DriveApp.getFileById(SStocopy.getId()).makeCopy(name, SSdestFolder);

Solution

  • Triggering a script to run on folder creation is more complex and tricky (for me anyway).

    How about a solution that eliminates manual folder creation in favor of just having a sheet (eg "MemberList") that's auto-populated with a list of folder names, and a separate folder (eg "Template") that contains all files you want copied to each member's subfolder?

    In this scenario, all you do is run an "Add Member" script that prompts you for the new member's name, and the script then automatically creates a new subfolder inside your parent "Members" folder using your prefix convention (001 John Doe) and copies over all files from the "Template" folder.

    That way if in future you need more than 2 files copied to the subfolder, you just add template files to the "Template" folder and they'll get copied.

    I created a sample sheet with instructions here. You can copy it and test by selecting "Add Member" from the custom Admin menu. First swap in your folder IDs in the script (see below). You'll just need to ensure you have a separate folder with your 2 template files.

    /**
     * @OnlyCurrentDoc
     */
    
    function onOpen() {
      var ui = SpreadsheetApp.getUi();
      ui.createMenu('Admin')
        .addItem('Add Member','createMember')
        .addToUi();
    }
    
    // ID of template folder which contains all files to copy for each member
    const FOLDER_TEMPLATE_ID = 'ENTER_YOUR_FOLDER_ID'
    // ID of parent folder where you want to add subfolders for each member
    const FOLDER_MEMBERS_ID = 'ENTER_YOUR_FOLDER_ID'
    
    function createMember() {
      var ui = SpreadsheetApp.getUi();
      var promptName = ui.prompt("Enter new member's name");
      var memberName = promptName.getResponseText();
    
      var sh = SpreadsheetApp.getActive().getSheetByName("FolderList");
      var row = sh.getLastRow()+1;
        if (row < 10) {
          var prefix = "00";
      } else if (row < 100){
          var prefix = "0";
      } else {
          var prefix = "";
      }
      var folderName = prefix + row + " " + memberName;
      
      sh.getRange(row,1).setValue(folderName);
        var newFolder = DriveApp.getFolderById(FOLDER_MEMBERS_ID).createFolder(folderName).getId();
        var fromFolder = DriveApp.getFolderById(FOLDER_TEMPLATE_ID);
        var toFolder = DriveApp.getFolderById(newFolder);
      copy(fromFolder, toFolder);
    }
    
    function copy(fromFolder, toFolder) {
      var files = fromFolder.getFiles()
      while (files.hasNext()) {
        var file = files.next();
        var filename = file.getName();
        file.makeCopy(filename,toFolder);
      }
    }