此处的工作代码: https://jsfiddle.net/nateomardavis/e0317gb6/
以下原始问题
如何从云端硬盘本身删除表单提交的文件?
我在梳理为什么Google表单将文件既提交到我的驱动器(不在文件夹中)又提交到自动生成的提交文件夹中时遇到了麻烦。
我已经能够将重命名的文件移动到新文件夹,并删除自动生成的提交文件夹中的副本。我无法弄清楚如何删除“驱动器”中列出的副本,而不是任何文件夹中的副本。
过程(编辑)
让我尝试进一步解释该过程。我有一个收集文件的表格。 Google会自动创建一个文件夹和子文件夹。我已经成功地重命名了提交的文件,将它们移动到新文件夹,并从Google生成的文件夹中删除了它们。但是,原始的未更改文件的副本将进入Google云端硬盘的根文件夹。步骤1-3(如下)将按预期工作。步骤4是我遇到问题的地方。
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
答案 0 :(得分:1)
这个答案怎么样?
从Google表单上传的流程如下。
在上述情况下,根文件夹中的文件与Google Form创建的文件夹中的文件不同。这样,您的“ SNIPPET”中的DriveApp.getRootFolder().removeFile(dFile)
无法正常工作。这是您发出脚本的原因。
为了删除在根文件夹中创建的文件,该解决方法如何?
不幸的是,无法从表单响应中检索原始文件名。但是复制到该表单创建的文件夹中的文件的文件名格式为{original filename} - ####.{extension}
。在这种解决方法中,将从该文件名中检索原始文件名,然后使用检索到的原始文件名将文件移至垃圾箱。
此示例脚本由可安装的表单提交触发器运行。因此,提交表单后,将运行脚本,并将上载的文件移至目标文件夹,并将根文件夹中的原始文件移至回收站。
在此示例脚本中,它假定该脚本是Google Form的容器绑定脚本。请注意这一点。
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)
,则在复制文件完成之前先移动文件。由此,发生错误。所以我用了。