搜索文件夹以将上传的文件移动到子文件夹 - Google 表单

问题描述 投票:0回答:1

我有一个收集一组个人详细信息的谷歌表格。他们选择了几个选项,最后上传了一个文件。我希望将这些文件上传到已在谷歌驱动器中创建的特定文件夹(基于表单选择选项)。我在论坛中找到了一个很好的答案,但是,子文件夹的搜索(没有下面的参考)对我来说有点模糊。

参考: 如何将文件上传从 Google 表单移动到 Google 云端硬盘中的特定文件夹和子文件夹

示例:某人填写表格Q1。 “1”,Q2。 “表格 1”<- the upload. My other options for Q1 are 2 and 3. Inside my custom "reports" folder (in goolge drive) I have placed the 1,2 and 3 folders. I want this particular upload to end up in the "1" folder. With the below code, the file gets uploaded to some other folder, i.e., Form uploads.

const PARENT_FOLDER_ID = "<folder ID>";

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 answer = response.getItemResponses()[0].getResponse() // text in first answer
  
    // 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.getFoldersByName(answer).next(); //I am stuck at this point, how can I search for the particular folder and push the file inside
      files.forEach((fileId) => {
        // Move each file into the custom folder
        DriveApp.getFileById(fileId).moveTo(subfolder);
      });
    }
  } catch (f) {
    Logger.log(f);
  }
};

google-apps-script google-drive-api google-forms
1个回答
0
投票

你有一个接受文件上传的谷歌表单。您希望表单上 Q#1 的答案将指定用户文件的上传文件夹。您已在 Google Drive 上创建文件夹“1”、“2”、“3”来保存上传内容。

  • 回答 1 的用户,应该将他们的文件上传放在文件夹名称 =“1”
  • 回答 2 的用户,应该将他们的文件上传放在文件夹名称 =“2”
  • 回答 3 的用户,应该将他们的文件上传放在文件夹名称 =“3”

这个问题的解决方案是链接到 Google 表单的电子表格,它接受表单提交的回复。当收到每个表单提交时,电子表格将运行一个脚本;该脚本会将文件上传到适当的文件夹。


安装:

  • 复制下面显示的所有代码并粘贴到项目编辑器中
  • 找到这条线
    const subfolderIDs = ["<<folderID_1>>","<<folderID_2>>","<<folderID_3>>"]
    • 用文件夹“1”、“2”和“3”中的每一个替换 folderId。
  • 手动运行
    createSpreadsheetOnFormSubmitTrigger()
    • 这样做一次(而且只做一次)。
    • 这将以编程方式创建
      onFormSubmit
      触发器

// create a trigger that will fire when the spreadsheet has a form submitted to it.
function createSpreadsheetOnFormSubmitTrigger(){
  var ss = SpreadsheetApp.getActiveSpreadsheet()
  ScriptApp.newTrigger("moveFileSubmission")
    .forSpreadsheet(ss)
    .onFormSubmit()
    .create();
}

function moveFileSubmission(e){
  // these are the sub-folders in the UPLOAD FOLDER that relate to the answers to Q1
  const subfolderIDs = ["1S0x9KcR1VgD00Op_G_lV9EnGwRmMuL2P","1VWae65JeDNbXgraIloPh-h9q2CVLXvD4","1m7CcLYcaGOtH_ojMR2RKEWqxEzt7L0Qi"]

  // get Event Objects and values 
  var response = e.values;
  // answer to Question 1
  var question1 = response[1]
  // the file url of the upload
  var fileUrl = response[2]

  // get the fileId from the fileUrl
  var fileId = getIdFromUrl(fileUrl)
  // Logger.log("DEBUG: question#1 answer = "+question1+", URL = "+fileUrl+", fileID = "+fileId)

  // get the subfolderID based on the answer to qn#1
  var subfolderID = subfolderIDs[(question1-1)]
  // Logger.log("DEBUG: the subfolder id = "+subfolderID)

  // get the subfolder
  var subfolder = DriveApp.getFolderById(subfolderID)

  // Move the uploaded file into the appropriate folder
  DriveApp.getFileById(fileId).moveTo(subfolder)
}


//Easiest way to get file ID from URL on Google Apps Script
// https://stackoverflow.com/a/16840612/1330560
function getIdFromUrl(url) { 
  return url.match(/[-\w]{25,}/); 
}

逻辑

  • const subfolderIDs
    - 目标 folderIds 在一个数组中(从零开始)。这使得获取问题 #1 的答案并选择适当的文件夹 ID 变得容易。
  • var response = e.values;
    -解决方案使用Event Objects
  • var question1 = response[1]
    &
    var subfolderID = subfolderIDs[(question1-1)]
    • 得到问题#1的答案,
    • 减1(对于从零开始的数组),
    • 从子文件夹 ID 数组中获取该值。
  • var fileUrl = response[2]
    - 上传文件的 url 包含在事件对象中,但目前无法通过 URL 获取文件。所以我们需要使用工具
    getIdFromUrl()
    从URL中提取FileID。
  • var subfolder = DriveApp.getFolderById(subfolderID)
    - 获取目标文件夹。
  • DriveApp.getFileById(fileId).moveTo(subfolder)
    - 将文件移动到适当的文件夹。
© www.soinside.com 2019 - 2024. All rights reserved.