Google Forms Rename “File upload” File to “Question - Submitter”

ぃ、小莉子 提交于 2019-12-14 03:13:26

问题


I am using Google Forms to collect images from team members and I want to ensure that each file that is uploaded to Google Forms and saved in Google Drive has the same naming convention.

There are five File upload questions that team members are asked to upload their images to. The files are placed into Google Drive folders with random file names followed by - firstName lastName. On an onFormSubmit trigger I would like to change the names of the user-provided files to fileUploadQuestionName - firstName lastName.

I am pretty new to Google Apps Script and I have no idea how to go about doing this. Any help would be greatly appreciated!


回答1:


You can change the name of the uploaded file on each form submit by the following process

  • retrieve the last form response onFormSubmit with form.getResponses()[LAST FORM SUBMISSION]
  • retrieve the ID of the uploaded file with getItemResponses()[QUESTION NUMBER].getResponse()
  • open the file ID with DriveApp and change its name as desired
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: If you want to change a posteriori the names of all the files submitted and not just the last files - you need to loop through all form responses:

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



回答2:


The easiest way to do this would likely be by either iterating through the specified folder in google drive on a time-based trigger and either checking if they meet your specified condition or moving them to a different folder after they're renamed.

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);
    }
  }
}

From there you'll need to add a time-based trigger to run every hour or day or minute depending on how critical it is to always find files of the correct name.

I can't find any documentation on accessing the file from within the response item on google's formApp documentation.



来源:https://stackoverflow.com/questions/57730428/google-forms-rename-file-upload-file-to-question-submitter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!