locke14
locke14

Reputation: 1383

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

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
[email protected]

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?

Upvotes: 1

Views: 2273

Answers (1)

jbra95
jbra95

Reputation: 909

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

}

Upvotes: 5

Related Questions