谷歌表单根据提交的值将文件上传到特定的新文件夹 [英] Google Form Upload files to specific new folder based on the value submitted

查看:21
本文介绍了谷歌表单根据提交的值将文件上传到特定的新文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 2 个字段的表单,假设表单的名称是 CV Drops

I have form with 2 fields, lets say the name of the form is CV Drops

  1. 姓名
  2. 上传文件按钮

因此,默认情况下,当人们上传他们的文件时,它会保存在我的 Google Drive 文件夹中 CV Drops 下.我想要的是根据它们在字段 NAME 中的输入将文件放置在子文件夹中.

So, by default when people are uploading their files it will be keep in my Google Drive under folder CV Drops. What I wanted is that the files are placed inside subfolder based on their input in field NAME.

我该怎么做?

推荐答案

我相信你现在的情况和目标如下.

I believe your current situation and goal as follows.

  • 您的 Google 表单有 2 个字段.
  • Your Google Form has 2 fields.
  1. 姓名
  2. 上传文件按钮(在这种情况下,可以上传多个文件.)

  • 两个字段都设置为必填字段.
  • 您想将上传的文件移动到具有第一个问题名称"答案中的文件夹名称的特定文件夹.在这种情况下,您希望将文件夹创建为文件夹中的子文件夹.
  • 为了实现您的目标,我想使用 Google Apps 脚本.

    In order to achieve your goal, I would like to use Google Apps Script.

    请将以下脚本复制并粘贴到 Google Form 的容器绑定脚本中.请设置您要创建的子文件夹的顶级文件夹 ID.如果要在根文件夹中创建子文件夹,请设置root.

    Please copy and paste the following script to the container-bound script of Google Form. Please set the top folder ID of the subfolders you want to create. If you want to create the subfolders in the root folder, please set root.

    function onFormSubmit(e) {
      const folderId = "###";  // Please set top folder ID of the destination folders.
    
      const form = FormApp.getActiveForm();
      const formResponses = form.getResponses();
      const itemResponses = formResponses[formResponses.length-1].getItemResponses();
    
      Utilities.sleep(3000); // This line might not be required.
    
      // Prepare the folder.
      const destFolder = DriveApp.getFolderById(folderId);
      const folderName = itemResponses[0].getResponse();
      const subFolder = destFolder.getFoldersByName(folderName);
      const folder = subFolder.hasNext() ? subFolder : destFolder.createFolder(folderName);
    
      // Move files to the folder.
      itemResponses[1].getResponse().forEach(id => DriveApp.getFileById(id).moveTo(folder));
    }
    

    2.安装 OnSubmit 触发器.

    请安装 OnSubmit 事件触发器作为可安装的触发器.参考

    为了测试示例脚本和触发器,请打开谷歌表单并输入名称并上传文件并提交.这样,脚本通过触发可安装的 OnSubmit 触发器来运行.并且,上传的文件被移动到创建的文件夹中,文件夹名称为Name".

    In order to test the sample script and trigger, please open the Google Form and put Name and upload the files and submit them. By this, the script is run by firing the installable OnSubmit trigger. And, the uploaded files are moved to the created folder which has the folder name of "Name".

    在此示例脚本中,当存在相同的文件夹名称时,文件将放入现有文件夹中.

    In this sample script, when the same folder name is existing, the files are put to the existing folder.

    • 这是一个简单的示例脚本.所以请根据您的实际情况进行修改.

    这篇关于谷歌表单根据提交的值将文件上传到特定的新文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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