Google form file upload: get filename instead of link to file

会有一股神秘感。 提交于 2020-01-24 20:12:26

问题


I have a simple google form which has a file upload option. The uploaded files are saved in my drive folder. I then have a simple script which executes on form submit to save the form values (upload filename, name and email) to a text file. Here's my app script to do that:

function onSubmit(e)
{
  var file = e.values[1]; // getting the link instead of the filename 
  var name = e.values[2];
  var email = e.values[3];

  var folderId = "0B6de05UZOb0y1ck4tV3BPWldpMU14SGZncnlHa0tzTXVoZFk";

  var folder = DriveApp.getFolderById(folderId);

  var content = file + "\n" + name + "\n" + email + "\n"

  var file = folder.createFile('My File.txt', content, MimeType.PLAIN_TEXT);  
}

Here's what the output file My File.txt looks like:

https://drive.google.com/open?id=1bVbNAnES33oY4gx8npdp51ZSMekQalk
My Name
my@email.com

My question is - instead of a link to the uploaded file, I'd like to have the actual filename. Is there a way I can get the filename from the form responses parameter e?


回答1:


If you have the file ID you can get the name of the file using the getName() method.

Documentation

Example

function driveFile(){

  //e.values[1] is apparently the file URL 
  var url = "https://drive.google.com/open?id=1bVbNAnES33oY4gx8npdp51ZSMekQalk"
  var id = url.split("=")[1];

  var file = DriveApp.getFileById(id);
  var fileName = file.getName();

}


来源:https://stackoverflow.com/questions/58427528/google-form-file-upload-get-filename-instead-of-link-to-file

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