问题
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