0

I have a Google Form which requires files to be uploaded. I have added a script which creates a sub-folder from a Form field (in this case a student number).

In this image some of the auto-generated sub-folders are visible, as well as the custom sub-folder (in this case for a student number 999999999

enter image description here

For the initial part of the form 4 files have to be uploaded. Once the form is submitted, there are now three copies of the files; one set in the root "My Drive", one set in the auto-generated Google Drive sub-folders, one set in the custom sub-folder (student number).

I used code I found on Stackoverflow and made minor modifications (Google Drive custom sub-folder address & Form Field to be used to create the custom sub-folder - How to Move File Uploads from Google Forms to a Specific Folder & Subfolders in Google Drive)

Here is my code:

 const PARENT_FOLDER_ID = "1x_SpJWrQ3FLuosjooPD3mNMVKphvQZMAf59Hd6O0HYwlguaxCzWaqRR3rxBkWI9P26zKx1CF";

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 StudentNumber = response.getItemResponses()[2].getResponse() // text in third answer
    
    const subfolderName = StudentNumber 
  
    // 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);
  }
};

How do I prevent/delete multiple copies of the same files when submitting a Form response? I only want files in the custom sub-folder.

vimuth
  • 5,064
  • 33
  • 79
  • 116
Ian
  • 1
  • Although I'm not sure whether I could correctly understand your question, for example, is this thread useful? https://stackoverflow.com/q/56171896/7108653 – Tanaike Jul 16 '22 at 00:18
  • Thank you for your reply. I am new to the functionality of scripting for Google Forms and have much to learn. My problem is similar to the link you directed me to - my uploaded files are saved to three places 1. Google auto-generated folder, 2. Google Drive root folder, 3. Custom sub-folder generated by the script I attached. I only want uploaded files to appear in the third option (Custom sub-folder). How do I remove the copies of the uploaded files from 1. and 2.? I am not renaming any files. – Ian Jul 19 '22 at 09:11
  • Thank you for replying. I deeply apologize that my comment was not useful for your situation. This is due to my poor skill. I deeply apologize for this again. I think that I have to study more. – Tanaike Jul 19 '22 at 23:06

0 Answers0