79171258

Date: 2024-11-08 19:06:33
Score: 1
Natty:
Report link

Move Multiple Files on FormSubmit

I modified your code this can now handle and move multiple files on formsubmit.

Here's the code:

function onFormSubmit(e) {

  const responses = e.values;
  const companyName = responses[1];
  const logoFileId = responses[2];
  const photoFileId = responses[3];
  const videoFileId = responses[4];

  const parentFolderId = 'paste-parent-folder-id-here';
  const parentFolder = DriveApp.getFolderById(parentFolderId);
  const companyFolder = parentFolder.createFolder(companyName);

  extractFileId(logoFileId, `${companyName}_Logo`);
  extractFileId(photoFileId, `${companyName}_Photo`);
  extractFileId(videoFileId, `${companyName}_Video`);

  function extractFileId(url, name) {
    const data = url;
    if (data.includes(',')) {
      const splitedData = data.split(',');
      for (let i = 0; i < splitedData.length; i++) {
        moveAndRenameFile(splitedData[i].split("=")[1], name)
      }
      return
    } else if (!data) {
      console.log("No Action taken");
      return;
    }
    moveAndRenameFile(data.split("=")[1], name)
    return
  }


  function moveAndRenameFile(fileId, newName) {
    try {
      if (fileId) {
        Logger.log(`Attempting to move file ID: ${fileId}`);
        const file = DriveApp.getFileById(fileId);
        file.setName(newName);
        file.moveTo(companyFolder);
      }
    } catch (error) {
      Logger.log(`Error with file ID ${fileId}: ${error.message}`);
    }
  }

}

Here is how you create a form question with attach multiple files.

Sample 1

Sample Output

Lime Husky

Lime Husky Child Folder

Lime Husky Child Folder Output


References:

Reasons:
  • Probably link only (1):
  • Contains signature (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Lime Husky