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

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

问题描述

我有2个字段的表单,可以说表单的名称是 CV Drops

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

  1. 名称
  2. 上传文件"按钮

因此,默认情况下,当人们上传文件时,该文件将保存在我的Google云端硬盘中的 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事件触发器作为可安装触发器.参考

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

    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.

    • 这是一个简单的示例脚本.因此,请根据您的实际情况对其进行修改.

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

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