Home > Software design >  Uploading Google Form Attachments to Specific Google Drive Subfolder
Uploading Google Form Attachments to Specific Google Drive Subfolder

Time:04-01

I am using this code snippet that I found online.

The google form has 2 portions. First is the name of destination subfolder. Second are the attachments.

But one problem is that every time I submit a response, it creates a duplicate subfolder of the same name.

function addFilesToDrive(e) {
  var answers = e.response.getItemResponses();
  var drive = DriveApp;
  var mainFolder = drive.getFolderById('Enter Folder ID here');
  var folderName = answers[0].getResponse();
  var folder = mainFolder.createFolder(folderName);
  var attachments = answers[1].getResponse();
  for (var file in attachments){
    var attachment = drive.getFileById(attachments[file]);
    folder.addFile(attachment);
  }
}

How to make it so that the files I upload are uploaded to the same subfolder without duplication?

Google Form

Duplicate Folder problem

CodePudding user response:

You can check if a folder with the same name exists this way:

function addFilesToDrive(e) {
  var answers = e.response.getItemResponses();
  var drive = DriveApp;
  var mainFolder = drive.getFolderById('Enter Folder ID here');
  var folderName = answers[0].getResponse();
 
  try {
    var folder = mainFolder.getFoldersByName(folderName).next();
  } catch (e) {
    var folder = mainFolder.createFolder(folderName);
  }

  var attachments = answers[1].getResponse();
  for (var file in attachments) {
    var attachment = drive.getFileById(attachments[file]);
    folder.addFile(attachment);
  }
}

CodePudding user response:

Move Folder

function movefolder() {
  const f4 = DriveApp.getFolderById("id");
  const f2 = DriveApp.getFolderById("id");
  f4.moveTo(f2);
}

moveTo

  • Related