google-apps-script - 我可以使用表单字段重命名使用 googlescript 上传的 pdf 或 doc 吗?
问题描述
我正在制作一个上传pdf或doc格式文件的表单。此表单具有其他字段,例如班级编号。课程名称....等 我可以使用表单字段来重命名上传文件吗?...附件是表单草稿
这是为了从我大学的工作人员上传数据。我已经处理了以前的建议,但仍然需要在表单字段上更改文件的名称。在此处输入图像描述
解决方案
没有用于重命名上传文件的专用表单字段,但您可以创建一个附加字段,提示用户输入文件应重命名的名称。
如果上传文件,则其在 Google 驱动器上的 URL 将插入到目标电子表格的相应字段中:
- 在第一步中,您需要从工作表中检索 URL 并提取 Id - 您可以执行后者,例如使用string.split()
- 在下一步中,您可以使用 DriveApp 方法添加和重命名文件DriveApp.getFileById(id).setName(name) +由此名称可以由您预先选择,也可以由表单提交者在指定的表单字段中输入。
根据我为您提供的先前解决方案,这里有一个修改,它将文件重命名为相应表单字段中输入的名称:
function myFunction() {
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheet=ss.getActiveSheet();
var lastRow=ss.getLastRow();
//the first column contains the timestamp
var department=sheet.getRange(lastRow,2).getValue();
var subjectCode=sheet.getRange(lastRow,3).getValue();
var courseCode=sheet.getRange(lastRow,4).getValue();
var courseNo=sheet.getRange(lastRow,5).getValue();
var newResponse=courseCode.toString()+courseNo.toString()+"-"+subjectCode.toString();
sheet.getRange(lastRow,8).setValue(newResponse);
var secodarySheetId;
switch(department) {
case "Physics":
secodarySheetId="XXX";//Paste here the Id of the destinationsheet in the Physics folder
break;
case "Chemistry":
secodarySheetId="XXX";//Paste here the Id of the destinationsheet in the Chemistry folder
break;
case "Math":
secodarySheetId="XXX";//Paste here the Id of the destinationsheet in the Maths folder
break;
}
var rowContents=sheet.getRange(lastRow,1,1,sheet.getLastColumn()).getValues();
SpreadsheetApp.openById(secodarySheetId).getSheetByName("Sheet1").appendRow(rowContents[0]);
//Here is the part retrieving the file, the desired new name and renaming the file
var url=sheet.getRange(lastRow,6).getValue();
var regex_ids = /\/file\/d\/([^\/]+)/;
var Id = url.split('=');//.match(/[-\w]{25,}/); //regex_ids.exec(url);
var newName=sheet.getRange(lastRow,7).getValue();
DriveApp.getFileById(Id).setName(newName)
}
推荐阅读
- php - 未捕获的错误:在 C 中找不到类 'PHPMailer\PHPMailer\Exception'
- jmeter - 有没有办法以编程方式确定 JMeter 是否在“功能测试模式”下运行?
- python - 将数据列表列表转换为python中的矩阵(检查内部)
- html - Angular Material 表单字段外观填充大小
- arrays - 检查字符串是否缩写并复制完整的字符串。(Vba)
- javascript - 无法在 Express JS 应用程序上设置 IP 地址
- javascript - 如何“加载”另一个 HTML 文件?
- class - Python 的类接口设计可以在 Rust 中镜像吗?
- android - Android 通知自定义视图:应用终止时无法点击自定义视图中的按钮
- r - 为什么 dplyr 会转换 POSIXct 对象