删除Google表单提交的文件 [英] Remove Google Form Submitted File

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

问题描述

这里的工作代码: https://jsfiddle.net/nateomardavis/e0317gb6 /

以下原始问题

如何从云端硬盘本身删除表单提交的文件?

How do I remove a form-submitted file from Drive itself?

我在梳理为什么Google表单将文件既提交到我的驱动器(不在文件夹中)又提交到自动生成的提交文件夹中时遇到了麻烦.

I'm having trouble sorting out why a google form is submitting files to both my drive (not in a folder) but also into an auto-generated submission folder.

我已经能够将重命名的文件移动到新文件夹并删除自动生成的提交文件夹中的副本.我无法弄清楚如何删除驱动器"中列出的副本,而不是任何文件夹中的副本.

I've been able to move the renamed file to a new folder and delete the copy in the auto-generated submission folder. I cannot figure out how to remove the copy that's just listed in "Drive", not in any folder.

过程(编辑)

让我尝试进一步解释该过程.我有一个收集文件的表格. Google会自动创建一个文件夹和子文件夹.我已经成功地重命名了提交的文件,将它们移动到新文件夹,并从Google生成的文件夹中删除了它们.但是,原始的,未更改的文件的副本将进入Google云端硬盘的根文件夹.步骤1-3(以下)将按预期工作.步骤4是我遇到问题的地方.

Let me try to explain the process more. I have a form that collects files. Google automatically makes a folder and sub-folders. I have successfully renamed the submitted files, moved them to a new folder, and deleted them from the Google-generated folder. However, a copy of the original, unchanged file is going to Google Drive, the root folder. Steps 1-3 (below) work as expected. Step 4 is where I'm running into issues.

  1. 正在上载到表单的原始文件.记下文件名.

  1. The original file being uploaded to a form. Note the file name.

Google生成的文件夹.该文件已提交到此文件夹.

The Google-generated folder. The file is submitted this folder.

新文件夹中的重命名文件.原始文件将从上面的文件夹中删除.

The renamed file in a new folder. The original file is deleted from the folder above.

原始文件现在显示在云端硬盘中,而不是在文件夹中,而是在其中.该文件的名称与最初上传的文件的名称相同.转到"passs"文件夹,然后从该文件夹中删除的那个.

The original file is now showing up in Drive, not in a folder but there. The name of this file is the same as the originally uploaded one. The one which went to the "passes" folder and was then deleted from that folder.

SNIPPET

//RENAME PASSES
if (itemResponses[f].getItem().getTitle() == "PASSES") { 
var files = itemResponses[f].getResponse();
//Logger.log(files.length);
if (files.length > 0) {
  for (var n in files) {
    var dFile = DriveApp.getFileById(files[n]);
    dFile.setName("LSS - " + year + " - " + teamName + " - " + "PASSES - " + today );
    teamFolder.addFile(dFile);   //MOVE SUBMITTED DOCUMENTS TO THAT FOLDER
    passesFolder.removeFile(dFile); //REMOVE FROM SUBMISSION FOLDER
    DriveApp.getRootFolder().removeFile(dFile) // (DOES NOT WORK) REMOVE FROM DRIVE FOLDER
    DriveApp.removeFile(dFile) // (DOES NOT WORK) REMOVE FROM DRIVE FOLDER
  }
}

完整代码

function getLastResponse() {

  var form = FormApp.openById('ID');

  var today = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "MM/dd/yyyy hh:mm a");
  var year = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "YYYY");
  Logger.log(today);

  var formResponses = form.getResponses();
  //Logger.log(formResponses.length);
  var formResponse = formResponses[formResponses.length-1];
  var respondentEmail = formResponse.getRespondentEmail()
  var itemResponses = formResponse.getItemResponses();
  Logger.log(itemResponses.length);
  var teamName = itemResponses[2].getResponse();
  //Logger.log("team name: " + teamName);


  //CHECK FOLDERS
   var dropbox = "Lititz Summer Showcase Team Check In (File responses)";
    var folder, folders = DriveApp.getFoldersByName(dropbox);

    var teamBox = teamName;
    var teamFolder, teamFolders = DriveApp.getFoldersByName(teamBox);

    var passesFolder = DriveApp.getFolderById('ID');
    var rosterFolder = DriveApp.getFolderById('ID');
    var teamInfoFolder = DriveApp.getFolderById('ID');
    var permissionToTravelFolder = DriveApp.getFolderById('ID');

    if (folders.hasNext()) { //CHECK IF DRIVE HAS FOLDER FOR FORM
      folder = folders.next();
    } else { //IF NOT CREATE FOLDER
      folder = DriveApp.createFolder(dropbox);
    }

    if (teamFolders.hasNext()) {  //CHECK IF FOLDER FOR TEAM EXISTS 
      teamFolder = teamFolders.next();
    } else { //IF NOT CREATE FOLDER
      teamFolder = folder.createFolder(teamBox);
      teamFolder.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.COMMENT);
    }

for (var f = 0; f < itemResponses.length; f++) {
    Logger.log(itemResponses[f].getItem().getType());
    Logger.log(itemResponses[f].getItem().getTitle());
  if (itemResponses[f].getItem().getType() == "FILE_UPLOAD") {
    Logger.log("THERE IS A FILE UPLOAD");

    //RENAME PASSES
    if (itemResponses[f].getItem().getTitle() == "PASSES") { 
    var files = itemResponses[f].getResponse();
    //Logger.log(files.length);
    if (files.length > 0) {
      for (var n in files) {
        var dFile = DriveApp.getFileById(files[n]);
        dFile.setName("LSS - " + year + " - " + teamName + " - " + "PASSES - " + today );
        teamFolder.addFile(dFile);   //MOVE SUBMITTED DOCUMENTS TO THAT FOLDER
        passesFolder.removeFile(dFile); //REMOVE FROM SUBMISSION FOLDER
        DriveApp.removeFile(dFile); // REMOVE FROM DRIVE FOLDER
      }
    }
    //RENAME ROSTER
    } else if (itemResponses[f].getItem().getTitle() == "ROSTER") {
      var files = itemResponses[f].getResponse();
    //Logger.log(files.length);
    if (files.length > 0) {
      for (var n in files) {
        var dFile = DriveApp.getFileById(files[n]);
        dFile.setName("LSS - " + year + " - " + teamName + " - " + "ROSTER - " + today );
        teamFolder.addFile(dFile);

      }
    }
   //RENAME TEAM INFO SHEET
   } else if (itemResponses[f].getItem().getTitle() == "TEAM INFO SHEET") {
      var files = itemResponses[f].getResponse();
    //Logger.log(files.length);
    if (files.length > 0) {
      for (var n in files) {
        var dFile = DriveApp.getFileById(files[n]);
        dFile.setName("LSS - " + year + " - " + teamName + " - " + "TEAM INFO SHEET - " + today );
        teamFolder.addFile(dFile);
      }
    }

  //RENAME PERMISSION TO TRAVEL
  } else if (itemResponses[f].getItem().getTitle() == "PERMISSION TO TRAVEL") {
      var files = itemResponses[f].getResponse();
    //Logger.log(files.length);
    if (files.length > 0) {
      for (var n in files) {
        var dFile = DriveApp.getFileById(files[n]);
        Logger.log(ownerEmail);
        dFile.setName("LSS - " + year + " - " + teamName + " - " + "PERMISSION TO TRAVEL - " + today );
        teamFolder.addFile(dFile);
      }
    }
    }
}//END 'IF FILE UPLOAD'
}//END FOR LOOP


}//END FUNCTION

推荐答案

此答案如何?

从Google表单上传的流程如下.

The flow of upload from Google Form is as follows.

  1. 在表单中上载文件时,文件将创建到根文件夹中.
  2. 提交表单后,通过将文件名重命名为由表单创建的文件夹来复制根文件夹中的文件.

在上述情况下,根文件夹中的文件与Google Form创建的文件夹中的文件不同.这样,您的"SNIPPET"中的DriveApp.getRootFolder().removeFile(dFile)无效.这是您发出脚本的原因.

In above case, the file in root folder is different from the file in the folder created by Google Form. By this, DriveApp.getRootFolder().removeFile(dFile) in your "SNIPPET" didn't work. This is the reason of your issue of script.

  • 您要删除保留在根文件夹中的文件.

为了删除在根文件夹中创建的文件,该解决方法如何?

In order to delete the file created in the root folder, how about this workaround?

不幸的是,无法从表单响应中检索原始文件名.但是,复制到该表单创建的文件夹中的文件的文件名具有{original filename} - ####.{extension}之类的格式.在这种解决方法中,将从该文件名中检索原始文件名,然后使用检索到的原始文件名将文件移至垃圾箱.

Unfortunately, the original filename cannot be retrieved from the form response. But the file which was copied to the folder created by the form has the filename of the format like {original filename} - ####.{extension}. In this workaround, the original filename is retrieved from this filename, and move the file to the trash using the retrieved original filename.

此示例脚本由可安装的表单提交触发器运行.因此,提交表单后,将运行脚本,并将上载的文件移至目标文件夹,并将根文件夹中的原始文件移至废纸..

在此示例脚本中,它假定该脚本是Google Form的容器绑定脚本.请注意这一点.

In this sample script, it supposes that the script is the container-bound script of Google Form. Please be careful this.

  1. 将脚本复制并粘贴到脚本编辑器后,请将目标文件夹ID设置为脚本.
  2. 请安装可安装表单提交触发器.如果已经安装了触发器,请删除它,然后重新安装.
  3. 请使用Google表单上传并提交文件.这样,脚本便开始运行.

脚本:

function formsubmit(e) {
  var destFolderId = "###"; // Destination folder ID

  if (e) {
    Utilities.sleep(3000); // This is required.
    var destfolder = DriveApp.getFolderById(destFolderId);
    var items = e.response.getItemResponses();
    for (var i = 0; i < items.length; i++) {
      if (items[i].getItem().getType() == "FILE_UPLOAD") {
        var files = items[i].getResponse();
        for (var j = 0; j < files.length; j++) {
          var file = DriveApp.getFileById(files[j]);
          var filename = file.getName();

          // Move uploaded file to the destination folder.
          var uploadedFile = DriveApp.getFileById(files[j]);
          var sourcefolder = uploadedFile.getParents().next();
          destfolder.addFile(file);
          sourcefolder.removeFile(file);

          // Retrieve original filename.
          var p1 = filename.split(" - ");
          var extension = p1[p1.length - 1];
          p1.pop();
          var name = p1.join(" - ");
          var p2 = "";
          if (extension.indexOf(".") > -1) {
            p2 = "." + extension.split(".")[1];
          }
          var orgFilename = name + p2;

          // Move uploaded file to the trash.
          var orgFiles = DriveApp.getRootFolder().getFilesByName(orgFilename);
          if (orgFiles.hasNext()) {
            var orgFile = orgFiles.next();
            orgFile.setTrashed(true);
          }
        }
      }
    }
  } else {
    throw new Error("This sample script is run by the installable form submit trigger.");
  }
}

注意:

  • 这是用于解释此变通办法的简单示例脚本.因此,请根据您的情况进行修改.
  • 在我的环境中,发现需要Utilities.sleep(3000).上传文件并复制文件后,将运行可安装的表单提交触发器.此时,如果未使用Utilities.sleep(3000),则在复制文件完成之前先移动文件.由此,发生错误.所以我用了.
    • 但是我不确定3秒的等待时间是否最佳.因此,如果在您的环境中发生错误,请对此进行修改.
    • 我认为,当上传大文件时,可能需要将此值设置为较大.或者我也认为,由时间驱动的触发器来运行脚本可能会更好.
    • Note:

      • This is a simple sample script for explaining this workaround. So please modify this for your situation.
      • In my environment, it was found Utilities.sleep(3000) was required. When the file is uploaded and the file is copied, the installable form submit trigger is run. At this time, if Utilities.sleep(3000) is not used, the file is moved before copying file is completed. By this, an error occurs. So I used it.
        • But I'm not sure whether the wait time of 3 seconds is the best. So if in your environment, an error occurs, please modify this.
        • I think that when a large file is uploaded, this value might be required to be large. Or also I think that running the script by the time-driven trigger might be better.
          • 但是我认为您也可以通过时间驱动的触发器来运行脚本.在这种情况下,您可以通过打开表单来检索响应项.为此,请根据您的情况选择.
          • Installable Triggers
          • setTrashed(trashed)
          • sleep(milliseconds)

          这篇关于删除Google表单提交的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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