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

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

问题描述

我在 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天全站免登陆