【问题标题】:How to Move File Uploads from Google Forms to a Specific Folder & Subfolders in Google Drive如何将文件上传从 Google 表单移动到 Google Drive 中的特定文件夹和子文件夹
【发布时间】:2022-02-09 21:21:48
【问题描述】:

当受访者通过 Google 表单上传文件时,该文件会存储在 Google 云端硬盘的固定文件夹中。所有文件都上传在同一个文件夹中,因此查看 Google Drive 中的文件,很难确定哪个受访者上传了哪组文件。

问题:我想增强脚本以根据用户在表单响应中的回答创建自定义文件夹名称。

谢谢。

const PARENT_FOLDER_ID = "<<Folder ID here>>";

const initialize = () => {
  const form = FormApp.getActiveForm();
  ScriptApp.newTrigger("onFormSubmit").forForm(form).onFormSubmit().create();
};

const onFormSubmit = ({ response } = {}) => {
  try {
    // Get a list of all files uploaded with the response
    const files = response
      .getItemResponses()
      // We are only interested in File Upload type of questions
      .filter(
        (itemResponse) =>
          itemResponse.getItem().getType().toString() === "FILE_UPLOAD"
      )
      .map((itemResponse) => itemResponse.getResponse())
      // The response includes the file ids in an array that we can flatten
      .reduce((a, b) => [...a, ...b], []);

    if (files.length > 0) {
      // Each form response has a unique Id
      const subfolderName = response.getId();
      const parentFolder = DriveApp.getFolderById(PARENT_FOLDER_ID);
      const subfolder = parentFolder.createFolder(subfolderName);
      files.forEach((fileId) => {
        // Move each file into the custom folder
        DriveApp.getFileById(fileId).moveTo(subfolder);
      });
    }
  } catch (f) {
    Logger.log(f);
  }
};

【问题讨论】:

    标签: google-apps-script google-sheets


    【解决方案1】:

    解决方案

    要以表格形式获得第一个问题的答复,您可以使用:

    const firstItemAnswer = response.getItemResponses()[0].getResponse()
    

    您也可以将提交的一些其他信息作为回复电子邮件或时间戳,但您必须启用Collect respondents' email addresses

    const userEmail = response.getRespondentEmail()
    const time = response.getTimestamp()
    

    一些注意事项

    如果修改触发函数onFormSubmit,则必须删除创建的trigger,然后再次调用initialize

    最终代码

    我已经使用这三个项目来创建子文件夹名称,但你可以保留你需要的。

    const PARENT_FOLDER_ID = "1LdF5khUZHhCRHqNNnN6lFE7fagFOS_xO";
    
    const initialize = () => {
      const form = FormApp.getActiveForm();
      ScriptApp.newTrigger("onFormSubmit").forForm(form).onFormSubmit().create();
    };
    
    const onFormSubmit = ({ response } = {}) => {
      try {
        // Get some useful data to create the subfolder name
        const firstItemAnswer = response.getItemResponses()[0].getResponse() // text in first answer
        const user = response.getRespondentEmail()  // email (Collect email addresses must be enabled)
        const time = response.getTimestamp()  // when the response was submited
        const subfolderName = firstItemAnswer + ' ' + user + ' ' + time 
      
        // Get a list of all files uploaded with the response
        const files = response
          .getItemResponses()
          // We are only interested in File Upload type of questions
          .filter(
            (itemResponse) =>
              itemResponse.getItem().getType().toString() === "FILE_UPLOAD"
          )
          .map((itemResponse) => itemResponse.getResponse())
          // The response includes the file ids in an array that we can flatten
          .reduce((a, b) => [...a, ...b], []);
    
        if (files.length > 0) {
          // Each form response has a unique Id
          const parentFolder = DriveApp.getFolderById(PARENT_FOLDER_ID);
          const subfolder = parentFolder.createFolder(subfolderName);
          files.forEach((fileId) => {
            // Move each file into the custom folder
            DriveApp.getFileById(fileId).moveTo(subfolder);
          });
        }
      } catch (f) {
        Logger.log(f);
      }
    };
    

    参考

    【讨论】:

    • 谢谢。它完美地工作! :) 对于那些可能想尝试的人,1. 一定要保存代码,2. 然后只运行一次 Initialize 函数。 3. 然后继续提交表单条目。注意:添加额外的触发器会创建多个文件夹(所以不要)
    猜你喜欢
    • 2023-01-16
    • 2013-03-01
    • 2016-07-26
    • 2018-10-22
    • 1970-01-01
    • 2018-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多