首页 > 解决方案 > 如何根据表单输入值从谷歌表单获取新的响应到不同的文件夹

问题描述

我有一个包含问题的表格

  1. 提单号
  2. 进口商代码
  3. 每个用户提交响应时上传文件,文件应保存在不同的文件夹中,文件夹名称应为问题“BL编号”的输入值。我们怎样才能使它成为可能?

标签: javascriptformsgoogle-apps-scriptgoogle-forms

解决方案


这在很大程度上取决于您的表单的外观。

这是一个如何完成的示例。

假设您有一个非常简单的表格,如下所示:

在此处输入图像描述

它有两个 'namedValues':FolderFile,它们将在下面的脚本中使用。

下一步:您必须在表单中添加一个“链接”电子表格。

在此处输入图像描述

在此电子表格中,您必须添加此脚本:

// names and IDs of your destination folders

var folders = {
  'aaa' : '###', // <-- you have to change ### with a real ID
  'bbb' : '###',
  'ccc' : '###'
}

function move_file_to_folder(e) {

  // get folder ID from folders object
  var folder_id = folders[e.namedValues['Folder'][0]]; // <-- the name from the named value 'Folder'

  // get file ID from the form
  var file_id = e.namedValues['File'][0].split('id=')[1]; // <-- the url from the named value 'File'

  // move the file into the folder
  DriveApp.getFileById(file_id).moveTo(DriveApp.getFolderById(folder_id));
}


// this function should be run just once to install the trigger

function install_OnFormSubmitTrigger() {
  var ss = SpreadsheetApp.getActive();
  ScriptApp.newTrigger('move_file_to_folder')
      .forSpreadsheet(ss)
      .onFormSubmit()
      .create();
}

之后,您必须运行该功能install_OnFormSubmitTrigger()来安装触发器。

move_file_to_folder()为了获得适当的访问权限,运行该函数可能也很有意义。它会给你一个错误。别介意。保持冷静并进行。

现在,每次您提交表单时,上传的文件都会进入具有相应名称的文件夹(在我的示例中为“aaa”、“bbb”、“ccc”)。


推荐阅读