0

Ex. someone submitting the form with a file upload and on form submit it creates a custom folder based on their form response (their name) on Google Drive with their submission in it.

Using this script, I can put all files into one folder, but I don't know enough about script to know where to change what the folder is named

const PARENT_FOLDER_ID = "<<Folder ID here>>";

const initialize = () => {
  const form = FormApp.getActiveForm();
  ScriptApp.newTrigger("onFormSubmit").forForm(form).onFormSubmit().create();
};

const onFormSubmit = ({ response } = {}) => {
  try {
    // Get a list of all files uploaded with the response
    const files = response
      .getItemResponses()
      // We are only interested in File Upload type of questions
      .filter(
        (itemResponse) =>
          itemResponse.getItem().getType().toString() === "FILE_UPLOAD"
      )
      .map((itemResponse) => itemResponse.getResponse())
      // The response includes the file ids in an array that we can flatten
      .reduce((a, b) => [...a, ...b], []);

    if (files.length > 0) {
      // Each form response has a unique Id
      const subfolderName = response.getId();
      const parentFolder = DriveApp.getFolderById(PARENT_FOLDER_ID);
      const subfolder = parentFolder.createFolder(subfolderName);
      files.forEach((fileId) => {
        // Move each file into the custom folder
        DriveApp.getFileById(fileId).moveTo(subfolder);
      });
    }
  } catch (f) {
    Logger.log(f);
  }
};
2
  • Welcome to Web Applications. Please show what you tried and add a brief description of your search efforts as is suggested in How to Ask.
    – Rubén
    Apr 29 '21 at 21:27
  • 1
    This is the line where the folder is named const subfolderName = response.getId(); If you need further help, please add more details regarding how you collect "their name" i.e. what is the question that asks for their name.
    – Rubén
    Apr 29 '21 at 22:35
1

You are using (verbatim) the script from the article by Digital Inspiration called How to Move Files Uploads from Google Forms to a Specific Folder in Google Drive. The nature of your question suggests that you have little or no experience with Apps Script; if this is so, then I suggest that you begin by reading developers.google.com/apps-script/guides/sheets.

Regarding your question, there are two instructions relating to assigning the folder into which uploads will be saved:

  • Create Parent Folder: To get started, go to your Google Drive and create a new folder (or use an existing folder). Open the folder and grab the ID of the folder from the browser’s address bar as shown in the screenshot.
  • Remember to replace the Folder Id in line #1 with the Id of the folder that you’ve created in the previous step.

Uploads for each unique response are saved to a custom folder within the parent folder. To quote the article: The name of the custom folder is the unique Response Id that Google Forms automatically assigns to every form submission.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.