Google表单文件上传:获取文件名而不是文件链接 [英] Google form file upload: get filename instead of link to file

查看:96
本文介绍了Google表单文件上传:获取文件名而不是文件链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Google表单,其中包含文件上传选项.上载的文件保存在我的驱动器文件夹中.然后,我有一个简单的脚本,该脚本在表单提交时执行,以将表单值(上传文件名,名称和电子邮件)保存到文本文件中.这是我的应用程序脚本:

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

这是输出文件My File.txt的样子:

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

我的问题是-我想要一个实际的文件名,而不是指向已上传文件的链接.有没有办法从表单响应参数e获取文件名?

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?

推荐答案

如果具有文件ID,则可以使用getName()方法获取文件名.

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

文档

示例

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

}

这篇关于Google表单文件上传:获取文件名而不是文件链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆