Google Forms uploaded file is stored two times in gDrive

EDIT: Solved! Head to the answers for the working code!

I have a Google Form with 3 fields and 1 upload file field, which is linked to a Google spreadsheet.

Every time I submit the form, my attachment is stored twice in my gDrive folders, one is in the root folder (the original file), and the other one is in the right Form_Name (File Responses), the renamed file (see script).

Both the Google Form and the Responses are inside a shared folder.

I have a script running on form submit (with a trigger) which renames the file before saving it, using some of the other submitted fields, tho only the one in Form_Name (File Responses) is renamed.

My need is to only keep the renamed file and delete the one in my gDrive root folder.

This is my script.

function onFormSubmit(e){ 

  var form = FormApp.getActiveForm();
  var formResponses = form.getResponses();
  var formResponse = formResponses[formResponses.length-1];
  var itemResponses = formResponse.getItemResponses();

  for (var j = 0; j < itemResponses.length; j++) {
    var itemResponse = itemResponses[j];

    switch(itemResponse.getItem().getTitle()){

      case "Time":
        var timeString = itemResponse.getResponse();
        break;

      case "Author":
        var authorString = "_" + itemResponse.getResponse();
        break;

      case "Type":
        var typeString = "_" + itemResponse.getResponse();
        break;  

      case "Setup.ini file":
        var fileId = itemResponse.getResponse();
        break;
    }

  }

  var fileNameString = timeString + typeString + authorString;

  Logger.log('renameFile('+fileId+','+fileNameString+');');
  console.log('renameFile('+fileId+','+fileNameString+');');

  renameFile(fileId,fileNameString);
}


function renameFile(id,fileName) {
  var file = DriveApp.getFileById(id);
  file.setName(fileName);
}

1 answer

  • answered 2019-08-24 09:25 GiLA3

    Working code! Thanks to this thread found by Tanaike -> stackoverflow.com/q/56171896

    function onFormSubmit(e){ 
    
      var form = FormApp.getActiveForm();
      var formResponses = form.getResponses();
      var formResponse = formResponses[formResponses.length-1];
      var itemResponses = formResponse.getItemResponses();
    
      for (var j = 0; j < itemResponses.length; j++) {
        var itemResponse = itemResponses[j];
    
        switch(itemResponse.getItem().getTitle()){
    
          case "Time":
            var timeString = itemResponse.getResponse();
            break;
    
          case "Author":
            var authorString = "_" + itemResponse.getResponse();
            break;
    
          case "Stint":
            var typeString = "_" + itemResponse.getResponse();
            break;  
    
          case "Setup.ini file":
            var fileId = itemResponse.getResponse();
            var uploadedFile = DriveApp.getFileById(fileId);
            var uploadedFileName = uploadedFile.getName();
            break;
        }
    
      }
    
      var fileNameString = timeString + typeString + authorString;
    
      Logger.log('renameFile('+fileId+','+fileNameString+');');
      Logger.log('thrashRootFile('+uploadedFileName+');');
      console.log('renameFile('+fileId+','+fileNameString+');');
      console.log('thrashRootFile('+uploadedFileName+');');
    
      renameFile(fileId,fileNameString);
      thrashRootFile(uploadedFileName);
    
    }
    
    
    function renameFile(id,fileName) {
      var file = DriveApp.getFileById(id);
      file.setName(fileName);
    }
    
    
    function thrashRootFile(fileName){
      var p1 = fileName.split(" - ");
      var extension = p1[p1.length - 1];
      p1.pop();
      var name = p1.join(" - ");
      var p2 = "";
      if (extension.indexOf(".") > -1) {
        p2 = "." + extension.split(".")[1];
      }
      var orgFilename = name + p2;
    
      // Move uploaded file to the trash.
      var orgFiles = DriveApp.getRootFolder().getFilesByName(orgFilename);
      if (orgFiles.hasNext()) {
        var orgFile = orgFiles.next();
        orgFile.setTrashed(true);
      }
    }