我有一个包含3个字段和1个上传文件字段的Google表单,该表单链接到Google电子表格。
每次提交表单时,附件都会两次存储在gDrive文件夹中,一个存储在根文件夹(原始文件)中,另一个存储在正确的Form_Name(文件响应)中,将文件重命名为(参见脚本)。
Google表单和响应都在共享文件夹中。
我有一个在表单提交(带有触发器)上运行的脚本,该脚本在保存文件之前使用其他一些提交字段对文件进行了重命名,只有在Form_Name(文件响应)中的那个文件被重命名了。
我只需要保留重命名的文件,然后删除gDrive根文件夹中的文件即可。
这是我的脚本。
function onFormSubmit(e){
var form = FormApp.getActiveForm();
var formResponses = form.getResponses();
var formResponse = formResponses[formResponses.length-1];
var itemResponses = formResponse.getItemResponses();
for (var j = 0; j < itemResponses.length; j++) {
var itemResponse = itemResponses[j];
switch(itemResponse.getItem().getTitle()){
case "Time":
var timeString = itemResponse.getResponse();
break;
case "Author":
var authorString = "_" + itemResponse.getResponse();
break;
case "Type":
var typeString = "_" + itemResponse.getResponse();
break;
case "Setup.ini file":
var fileId = itemResponse.getResponse();
break;
}
}
var fileNameString = timeString + typeString + authorString;
Logger.log('renameFile('+fileId+','+fileNameString+');');
console.log('renameFile('+fileId+','+fileNameString+');');
renameFile(fileId,fileNameString);
}
function renameFile(id,fileName) {
var file = DriveApp.getFileById(id);
file.setName(fileName);
}
答案 0 :(得分:0)
工作代码!感谢Tanaike找到此线程-> stackoverflow.com/q/56171896
function onFormSubmit(e){
var form = FormApp.getActiveForm();
var formResponses = form.getResponses();
var formResponse = formResponses[formResponses.length-1];
var itemResponses = formResponse.getItemResponses();
for (var j = 0; j < itemResponses.length; j++) {
var itemResponse = itemResponses[j];
switch(itemResponse.getItem().getTitle()){
case "Time":
var timeString = itemResponse.getResponse();
break;
case "Author":
var authorString = "_" + itemResponse.getResponse();
break;
case "Stint":
var typeString = "_" + itemResponse.getResponse();
break;
case "Setup.ini file":
var fileId = itemResponse.getResponse();
var uploadedFile = DriveApp.getFileById(fileId);
var uploadedFileName = uploadedFile.getName();
break;
}
}
var fileNameString = timeString + typeString + authorString;
Logger.log('renameFile('+fileId+','+fileNameString+');');
Logger.log('thrashRootFile('+uploadedFileName+');');
console.log('renameFile('+fileId+','+fileNameString+');');
console.log('thrashRootFile('+uploadedFileName+');');
renameFile(fileId,fileNameString);
thrashRootFile(uploadedFileName);
}
function renameFile(id,fileName) {
var file = DriveApp.getFileById(id);
file.setName(fileName);
}
function thrashRootFile(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);
}
}