Google 表单将“文件上传”文件重命名为“问题 - 提交者”

Use*_*erX 1 google-apps-script google-forms

我正在使用 Google 表单收集团队成员的图像,我想确保上传到 Google 表单并保存在 Google 云端硬盘中的每个文件都具有相同的命名约定。

团队成员需要将图像上传到五个文件上传问题。这些文件被放置在 Google Drive 文件夹中,文件名是随机的,后跟- firstName lastName. 在onFormSubmit触发器上,我想将用户提供的文件的名称更改为fileUploadQuestionName - firstName lastName.

我对 Google Apps 脚本还很陌生,我不知道如何去做。任何帮助将不胜感激!

zig*_*hka 5

您可以通过以下过程更改提交的每个表单上上传的文件的名称

  • 检索最后一个表单响应onFormSubmitform.getResponses()[LAST FORM SUBMISSION]
  • 检索上传文件的 IDgetItemResponses()[QUESTION NUMBER].getResponse()
  • 使用 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);
}
Run Code Online (Sandbox Code Playgroud)

PS:如果您想更改后验所有提交文件的名称,而不仅仅是最后一个文件的名称 - 您需要循环遍历所有表单响应:

for(var i=0;i<length;i++){ 
 var id=form.getResponses()[i].getItemResponses()[QUESTION NUMBER].getResponse();
  ...
 ...
  }
Run Code Online (Sandbox Code Playgroud)