Google Script - Create a Folder from Form Entry Data

Just put together this quick little script as we needed a folder created upon a form entry. In our case, we needed a folder created with the name of the person, so we are pulling the name entry from the form and setting that as the folder name.  This also includes how to direct it where to save these folders so they are sub-folders of an existing folder and not cluttering up the root level of your Drive.


Thought I would share in case someone else needs something similar!

1. Create your form.
You may want to enter 1 sample entry so you can test your script later. 

2. Create the form responses spreadsheet.
This will house the data from the form entries - you will be connecting the script to this sheet.

3. Open up the Script editor 
Go to the Tools Menu and select Script editor.


4. Replace the code.
Replace the default code -

with this - (all the text highlighted in gray include the final bracket) -

function createStudentFolder() {
 

// identify the sheet where the data resides
var ss = SpreadsheetApp.getActive();
var names = ss.getSheetByName("Sheet with Data");
 

//identify the cell that will be used to name the folder 
var studentName = names.getRange(names.getLastRow(), 4).getValue(); 
 

//identify the parent folder the new folder will be in
var parentFolder=DriveApp.getFolderById("Parent Folder ID");
 

//create the new folder
var newFolder=parentFolder.createFolder(studentName);
 
}


5. Enter Data Sheet Name
If you are using the default response entry sheet, you will substitute Sheet with Data with Form Responses 1- if you have named the sheet something else, use the exact name (with spaces if used)

6. Identify Cell with Folder Name 
 In this line -
 var studentName = names.getRange(names.getLastRow(), 4).getValue();
you will replace 4 with the correct column, starting with column A being 1. 

7. Identify the Parent Folder
Unless you want all these folders to be at the root level of your GDrive, you will need to establish the parent folder they should reside in.  To get the folder ID, find the folder in your Drive (it should be the end of the breadcrumb trail across the top).  The ID will be at the end of the URL after "folder/" (I've blurred mine out, but you do need the entire ID) -


8. Set Your Script Trigger
Go to the Edit Menu and select Current project's triggers.  Set a trigger to make your script run automatically (unless you really like opening it up all the time...).  Since the script refers to the "Last Row," this is one that should run on Form Submit - or be modified to handle multiple rows at one time.
9. Debug (test your accuracy)
If you click on the bug icon, it will debug your script.  If something needs fixing, it will give information at the bottom of the window.  If all is right with the world, it will run and nothing will happen.


That's it!  Go and test it - make sure to add a form entry so it has data to use in naming the folder.  You can either wait for the trigger to run it, or you can manually run it with the play icon while looking at the script.

Comments

Popular posts from this blog

Google Script - Create a Drop-Down List from Spreadsheet Column

Google Sheets - Flipping Your Rows & Columns

Google Forms - Copy Summary Graph/Diagram to Use Elsewhere

Google Script - Create Calendar Events from Spreadsheet Data

Google - Reset a Form for Reuse with the new School Year