【问题标题】:Google Forms Rename "File upload" File to "Question - Submitter"Google 表单将“文件上传”文件重命名为“问题 - 提交者”
【发布时间】:2020-10-28 10:40:03
【问题描述】:
我正在使用 Google 表单从团队成员那里收集图像,并且我想确保上传到 Google 表单并保存在 Google 云端硬盘中的每个文件都具有相同的命名约定。
有五个文件上传问题要求团队成员上传他们的图片。这些文件以随机文件名后跟- firstName lastName 放置到Google Drive 文件夹中。在 onFormSubmit 触发器上,我想将用户提供的文件的名称更改为 fileUploadQuestionName - firstName lastName。
我对 Google Apps 脚本很陌生,我不知道如何去做。任何帮助将不胜感激!
【问题讨论】:
标签:
google-apps-script
google-forms
【解决方案1】:
您可以通过以下过程在每个表单提交上更改上传文件的名称
- 使用
form.getResponses()[LAST FORM SUBMISSION]检索最后一个表单响应onFormSubmit
- 用
getItemResponses()[QUESTION NUMBER].getResponse()检索上传文件的ID
- 使用 DriveApp 打开文件 ID 并根据需要更改其名称
function myFunction() {
var form=FormApp.getActiveForm();
// returns the total number of form submissions
var length=form.getResponses().length;
//replace QUESTION NUMBER through the index of the question prompting the file upload - keep in mind that the first question has the index 0
var id=form.getResponses()[length-1].getItemResponses()[QUESTION NUMBER].getResponse();
//getResponses()[length-1] retrieves the last form response, accounting for the fact that the first index is zero and hte last length-1
//gets the name of the question
var fileUploadQuestionName=form.getResponses()[length-1].getItemResponses()[QUESTION NUMBER].getItem().getTitle();
//accesses the uploaded file
var file=DriveApp.getFileById(id);
name = file.getName();
//changes the file name
var name = fileUploadQuestionName+' - '+name.split(' - ')[1]
file.setName(name);
}
PS:如果您想事后更改所有提交文件的名称,而不仅仅是最后一个文件的名称 - 您需要遍历所有表单响应:
for(var i=0;i<length;i++){
var id=form.getResponses()[i].getItemResponses()[QUESTION NUMBER].getResponse();
...
...
}
【解决方案2】:
执行此操作的最简单方法可能是在基于时间的触发器上遍历谷歌驱动器中的指定文件夹,并检查它们是否满足您指定的条件,或者在重命名后将它们移动到不同的文件夹。
function checkFile()
{
var files = DriveApp.getFolderById('ID of Folder').getFiles();
// you can find this by going to form responses and clicking the link to an attached file and copying the id from the URL
// https://drive.google.com/drive/folders/xxxxxxxxxxxxxxxxxxxxxxxxxxx
var fileUploadQuestionName = 'Test';
while(files.hasNext())
{
var file = files.next();
var name = file.getName();
if(name.indexOf(fileUploadQuestionName) == -1)
{
name = fileUploadQuestionName+' - '+name.split('-')[1]
file.setName(name);
}
}
}
从那里您需要添加一个基于时间的触发器,以每小时、每天或每分钟运行一次,具体取决于始终找到正确名称的文件的重要性。
我在 google 的 formApp 文档的响应项中找不到有关访问该文件的任何文档。