如何将多个文件上传保存在特定的文件夹中? [英] How to save multiple file upload in a specific folder?

查看:75
本文介绍了如何将多个文件上传保存在特定的文件夹中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Google表单上创建了一个自定义表单,其中包含一个上载文件问题(允许多个文件).提交表单时,我试图运行一个脚本,该表单创建一个文件夹到云端硬盘中的特定目标位置,并保存在此文件夹中上传的文件.但是我不知道如何检索上载的文件的ID并将其移动到新文件夹中.谢谢

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??
}

我想获取文件的ID,但是使用.getItem().getID()方法,我得到的像是1.148501776E9还有如何循环浏览上传的文件?

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?

推荐答案

  • 通过Google表单上传文件时,您想将上传的文件移动到特定文件夹中.
  • 您已经为 onFormSubmit()安装了OnSubmit事件触发器.
  • 您已经具有目标文件夹的文件夹ID.
    • 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.

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

      在使用此脚本之前,请将目标文件夹ID设置为 var folderId ="###" .

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

      修改脚本后,请删除一次OnSubmit事件触发器,然后重新安装触发器.那时,如果显示授权屏幕.请授权.这样,脚本就可以工作了.请注意这一点.

      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);
            }
          }
        }
      }
      

      注意:

      • 在我的测试案例中,当未使用 Utilities.sleep(3000)时,会出现错误,因为在文件上传未完成之前尝试移动文件.所以我说了.如果在您的环境中没有它,则不会发生任何错误,请删除它.

        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.
        • 如果我误解了您的问题,而这不是您想要的结果,我深表歉意.

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

          这篇关于如何将多个文件上传保存在特定的文件夹中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆