Google Form Upload files to specific new folder based on the value submitted

I believe your current situation and goal as follows.

  • Your Google Form has 2 fields.
    1. Name
    2. Uploaded files button (In this case, multi files can be uploaded.)
  • Both fields are set as the required fields.
  • You want to move the uploaded files to the specific folder which has the folder name from the answer of 1st question “Name”. In this case, you want to create the folder as the subfolder in a folder.

In order to achieve your goal, I would like to use Google Apps Script.

Usage:

1. Sample script.

Please copy and paste the following script to the container-bound script of Google Form. Please set the top folder ID of the subfolders you want to create. If you want to create the subfolders in the root folder, please set root.

function onFormSubmit(e) {
  const folderId = "###";  // Please set top folder ID of the destination folders.

  const form = FormApp.getActiveForm();
  const formResponses = form.getResponses();
  const itemResponses = formResponses[formResponses.length-1].getItemResponses();

  Utilities.sleep(3000); // This line might not be required.

  // Prepare the folder.
  const destFolder = DriveApp.getFolderById(folderId);
  const folderName = itemResponses[0].getResponse();
  const subFolder = destFolder.getFoldersByName(folderName);
  const folder = subFolder.hasNext() ? subFolder.next() : destFolder.createFolder(folderName);

  // Move files to the folder.
  itemResponses[1].getResponse().forEach(id => DriveApp.getFileById(id).moveTo(folder));
}

2. Install OnSubmit trigger.

Please install OnSubmit event trigger as the installable trigger. Ref

3. Test script.

In order to test the sample script and trigger, please open the Google Form and put Name and upload the files and submit them. By this, the script is run by firing the installable OnSubmit trigger. And, the uploaded files are moved to the created folder which has the folder name of “Name”.

In this sample script, when the same folder name is existing, the files are put to the existing folder.

Note:

  • This is a simple sample script. So please modify it for your actual situation.

References:

Leave a Comment