AppScript creates two folders with createfolder function

74 Views Asked by At

I wrote a script for a googleform where you answer some questions and attach a file. According to the answers the submitted file is moved to a destinated folder with following logic (path: Firstanswer/secondanswer/thirdanswer/file. If one of these folders does not exist, a new (sub)folder according to the answers will be created.(function getsubFolder_) The script works, just for the fact that if a specific subfolder does not exits, the script will sometimes(not always) create two folders within the getsubfolder_ function with the same name (according to one of the answers). (could happen with either first,second or thirdanswer subfolder creation). It seems too happen randomly, but not always.

Here is the core of the code.

const onFormSubmit = ({ response } = {}) => {
 
  try {
    const firstItemAnswer = response.getItemResponses()[0].getResponse() 
    const secondItemAnswerfull = response.getItemResponses()[1].getResponse();
    const secondItemAnswer = secondItemAnswerfull.split(' ')[0]; 
    const thirdItemAnswer = response.getItemResponses()[2].getResponse() ;

const files = response
      .getItemResponses()
      .filter(
        (itemResponse) =>
          itemResponse.getItem().getType().toString() === "FILE_UPLOAD"
      )
      .map((itemResponse) => itemResponse.getResponse())
      .reduce((a, b) => [...a, ...b], []);
if (files.length > 0) {
      const parentFolder = DriveApp.getFolderById(PARENT_FOLDER_ID);
      
    const newparent1 = getSubFolder_(parentFolder, firstItemAnswer);
    const newparent2 = getSubFolder_(newparent1, secondItemAnswer);
    const newparent3 = getSubFolder_(newparent2, thirdItemAnswer);
 files.forEach((fileId) => {
        // Move each file into the custom folder
        DriveApp.getFileById(fileId).moveTo(newparent3);
        DriveApp.getFileById(fileId).setName(firstItemAnswer + "_" + secondItemAnswer + "_Month_" + thirdItemAnswer);
      });
    }
  } catch (f) {
    Logger.log(f);
  }
};

//function to create folder if not exist
function getSubFolder_(objParentFolder, subFolderName) {
  const subFolders = objParentFolder.getFolders();
  while (subFolders.hasNext()) {
    let folder = subFolders.next();
    if (folder.getName() === subFolderName) {
      return folder;
    }
  }
  return objParentFolder.createFolder(subFolderName);
}

As said, other then sometimes randomly creating a duplciate folder, there is no issue. The most confusing part for me is, that if the duplication happens on the firstanswer, the script will create all the subfolders in both folders, but it will only move the file to the last one createt and not the duplicate. So my question would be.

  1. How and why does this happen? can it be avoided to randomly created duplicate folders, as at least for me, what seems to be no logical reason?
  2. Why does the script follow through to create subfolders for second and thirdanswer in case first answer folder was duplicated ( I assume it might has something to do that im referencing the object.Maybe i should return getid() from getsubfolder_ function instead, but dont think it would solve duplicating problem?)

Thank you

0

There are 0 best solutions below