0

When a respondent uploads a file through Google Forms, the file are stored in a fixed folder on Google Drive. All files are uploaded in the same folder and, thus looking at the file in Google Drive, it is difficult to determine which respondent has uploaded which set of files.

Question: I would like to enhance the script to create custom folder names based on the user’s answers in the form response.

Thanks.

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);
  }
};

1 Answers1

0

Solution

To get the response of the first question in the form you can use:

const firstItemAnswer = response.getItemResponses()[0].getResponse()

You also can take some other information about the submit as the respondent email or the timestamp, but you have to enable Collect respondents' email addresses

const userEmail = response.getRespondentEmail()
const time = response.getTimestamp()

Some notes

If you modify the trigger function onFormSubmit, you will have to delete the created trigger and call initialize again.

Final code

I have taken the three items to create the subfolder name but you can keep the ones you need.

const PARENT_FOLDER_ID = "1LdF5khUZHhCRHqNNnN6lFE7fagFOS_xO";

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

const onFormSubmit = ({ response } = {}) => {
  try {
    // Get some useful data to create the subfolder name
    const firstItemAnswer = response.getItemResponses()[0].getResponse() // text in first answer
    const user = response.getRespondentEmail()  // email (Collect email addresses must be enabled)
    const time = response.getTimestamp()  // when the response was submited
    const subfolderName = firstItemAnswer + ' ' + user + ' ' + time 
  
    // 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 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);
  }
};

Reference

fullfine
  • 1,371
  • 1
  • 4
  • 11
  • Thank you. It works perfectly! :) For those who may want to try, 1. Be sure to save the code and 2. then run the Initialize function once only. 3. Then proceed to submitting a form entry. Note: Adding an extra trigger will create multiple folders (so dont) – King Michael King Mar 03 '21 at 13:34