Criagle
Criagle

Reputation: 23

Automatically change the name of destination folder based on Google Form answer

I have minimal experience coding, but I'm trying to alter this snippet to change the destination folder's name to a question response in my google form. The code works as intended and creates a folder for each individual response's file upload, but the folder's name is the response ID. I see where it does that in the code, but I'm unsure how to change it to the content of an answer within the form response. I want to change the folder name to be created as the response to a question that asks users their name.

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'm sorry in advance for my inexperience. I'm trying to understand App script better this summer, so as an adjacent question, if anyone has any resources I can use to help me, that would be amazing as well.

Upvotes: 0

Views: 587

Answers (1)

Tanaike
Tanaike

Reputation: 201428

I believe your goal as follows.

  • You want to create new folder by retrieving the folder name from the question, and put the uploaded files to the created folder.

Modification points:

  • In this case, at first, add new question for inputting the folder name to Google Form.
    • As a test, the title of this question is subfolderName.
  • In your script, in order to retrieve the file IDs, filter, map and reduce are used. In this modification, in order to retrieve the file IDs and the folder name, I use reduce. By this, I thought that the process cost can be reduced a little.

When above points are reflected to your script, it becomes as follows.

Modified script:

Before you test this modified script, please confirm whether the question for inputting the folder name as the title of subfolderName again.

From:

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], []);

To:

const {files, subfolderName} = response
  .getItemResponses()
  .reduce((o, e) => {
    const item = e.getItem();
    if (item.getType().toString() === "FILE_UPLOAD") {
      o.files = o.files.concat(e.getResponse());
    } else if (item.getTitle() === "subfolderName") {
      o.subfolderName = e.getResponse();
    }
    return o;
  }, {files: [], subfolderName: ""});
  • By above modification, files and subfolderName are the file IDs and the folder name, respectively.
  • In this sample, subfolderName is used for the title of the question for inputting the folder name on Google Form. When you want to modify this, please modify subfolderName of if (item.getTitle() === "subfolderName") for your actual situation.

Reference:

Upvotes: 1

Related Questions