这是一个基本的谷歌表格,记录非营利性住房的费用。下面列出了形式和目标的领域。我试图做到这一点,以便在提交时,使用表单本身的字段重命名上传的文件。我想使用的字段是一个日期选择器、两个字符串下拉列表和一个由数字组成的简短答案。事实上,谷歌通过在末尾附加“-上传者名称”来重命名文件。我也想删除它。
我能够找到一些答案,可以通过手动触发追溯重命名所有上传,但没有任何东西可以自动执行此操作。
字段详情
目标文件名:提交日期(YYYYMMDD)_personName_expenseType_expenseAmount
非常感谢您的帮助!
这不完整,我不知道获取字段的正确方法
function onFormSubmit(e) {
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
var file=DriveApp.getFileById(id);
// store filename in variable
name = file.getName();
//Removes the built in Name added by google
var originalname = name.split(' - ')[0]
// !! Missing, get responses from specific fields, including converting date picker into yyyymmdd format
var newname = datefield+namefield+typefield+originalname
file.setName(name);
}
示例电子表格添加了两列,显示原始文件名以及我希望将其重命名为的内容,这不必显示在工作表上,只需在谷歌驱动器中重命名即可。
您想要更改从 Google 表单上传的文件的文件名。
以下答案的一些值得注意的方面是:
onFormSubmit
function updateFileName(e) {
Logger.log(JSON.stringify(e)) // DEBUG
var values = e.values
//get the file details
var fileURL = values[5]
Logger.log("DEBUG: the file url = "+fileURL)
var id = fileURL.split("=")[1]
Logger.log("DEBUG: the file ID = "+id)
var file = DriveApp.getFileById(id)
var uploadFileName = file.getName()
Logger.log("DEBUG: the existing file name = "+uploadFileName)
// get the file name variables
var submittedDate = values[2].toString()
var yyyy = submittedDate.split("/")[2]
var mm = submittedDate.split("/")[0]
// ensure leading zero
if (mm.length == 1){
mm = "0"+mm
}
var dd = submittedDate.split("/")[1]
// ensure leading zero
if (dd.length == 1){
dd = "0"+dd
}
var personName = values[1]
var expenseType = values[3]
var expenseAmount = values[6]
var newName = yyyy+mm+dd+"_"+personName+"_"+expenseType+"_"+expenseAmount
Logger.log("DEBUG: the new name = "+newName)
// save the new file name
file.setName(newName)
// include this lines if you want to confirm the file name chnage
var newfile = DriveApp.getFileById(id)
var revisedFileName = file.getName()
Logger.log("DEBUG: the updated file name = "+revisedFileName)
}
样本 - 链接表
示例 - 执行日志