Google Form: How to save multiple file upload in a specific folder?

ε祈祈猫儿з 提交于 2020-08-03 05:48:27

问题


I have created a custom form on Google Form which contains an Upload File Question (allowing multiple files). I'm trying to run a script when submitting the form that creates a folder into a specific destination in Drive and that it saves the files uploaded in this one. But I have no idea how to retrieve the ID of the files uploaded and move them into the new folder. Thank you

I managed to create the new folder, but I'm not able to select the uploaded file and save them (or move them) to the target folder. I'm not posting the creating folder code.

function onFormSubmit(e) {
  var form = FormApp.getActiveForm();
  var formResponses = form.getResponses();
  var formResponse = formResponses[formResponses.length-1];
  var itemResponses = formResponse.getItemResponses();
  var itemResponse = itemResponses[1];
  var uploadedfile = itemResponse.getResponse();

  //now??
}

I would like to get the ID of the file but using the .getItem().getID() method I get something like 1.148501776E9 Also how can I loop through the uploaded files?


回答1:


  • When the files are uploaded by Google form, you want to move the uploaded files to the specific folder.
  • You have already installed the OnSubmit event trigger for onFormSubmit().
  • You have already had the folder ID of the destination folder.

If my understanding is correct, how about this modification? Please think of this as just one of several answers.

Modification points:

  • .getItem().getId() is the item's unique identifier.
  • getResponse() of itemResponses has the file IDs of uploaded files. You can use this.

Modified script:

Before you use this script, please set the destination folder ID to var folderId = "###".

When you modified the script, please delete the OnSubmit event trigger once and install the trigger again. At that time, if the authorization screen is displayed. Please authorize it. By this, the script works. Please be careful this.

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

  // I modified below script.
  Utilities.sleep(3000);

  var folderId = "###";  // Please set folder ID of the destination folder.

  var destfolder = DriveApp.getFolderById(folderId);
  for (var i = 0; i < itemResponses.length; i++) {
    if (itemResponses[i].getItem().getType() == "FILE_UPLOAD") {
      var ids = itemResponses[i].getResponse();
      for (var j = 0; j < ids.length; j++) {
        var file = DriveApp.getFileById(ids[j]);
        destfolder.addFile(file);
        file.getParents().next().removeFile(file);
      }
    }
  }
}

Note:

  • In my test case, when Utilities.sleep(3000) is not used, there was the case that an error occurs because the file is tried to move before the upload of file is not finished. So I put it. If in your environment, no error occurs without it, please remove it.

References:

  • getParents()
  • addFile()
  • removeFile()

If I misunderstood your question and this was not the result you want, I apologize.




回答2:


Google stores the uploaded files in the following way:

  1. It creates a folder "Placeholder_for_Name_of_the_Form"(File responses) and subfolder: "Placeholder_for_Name_of_Question"(File responses) and stores all uploaded files there.
  2. If you link the form to a spreadsheet, the spreadsheet will contain the URL to the uploaded file (which contains the ID) as an answer to the question. You can access these data also programmatically with Apps Script (relevant methods: openSpreadsheetbyId(); getRage(); getValues(),...).

This information could be useful for you if you want to retrieve the data at a different time point than onFormSubmit().



来源:https://stackoverflow.com/questions/56952300/google-form-how-to-save-multiple-file-upload-in-a-specific-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!