-1

Relatively new here, do forgive any formatting errors.

So currently I've set up a Form to automatically create a subfolder into a designation parent folder, with all the attachments uploaded in the form contained in the subfolder.

That's working well, but I have two problems now:

  1. The subfolder created is automatically given a generic ID by google, and I would instead like it to be named based on a response from the form
  2. The script creates about 6 subfolders from one form submission, though only one subfolder contains the attachments from the form.

I have very little programming knowledge, but would be keen to understand, if this is doable with my rudimentary knowledge.

Thanks!


const PARENT_FOLDER_ID = "";

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);
  }
};
Rubén
  • 29,320
  • 9
  • 61
  • 145

0 Answers0