'Google form script -> how to change subfolder name + generate PDF of responses

I am an amateur when it comes to code (I work with marketing) and tried tinkering with a script for Google Forms and ended up not finding a workable solution.

What I aim to achieve:

  1. Currently, the script creates subfolders with a unique ID, while I'd prefer for it to be based on one of the responses
  2. Generate a PDF with the responses for each form entry, which will be in the same subfolder as the attachments

As standing, I have this code to send attachments to a shared drive at my company:

const PARENT_FOLDER_ID = 'folderIDhere';
 
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);
  }
};

(I have removed the unique ID from the first line for privacy concerns). The code is from this blog post: https://www.labnol.org/file-uploads-folder-google-forms-201226

I'd like advice on how I can change this script to achieve both goals listed above.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source