首页 > 解决方案 > 我可以使用表单字段重命名使用 googlescript 上传的 pdf 或 doc 吗?

问题描述

我正在制作一个上传pdf或doc格式文件的表单。此表单具有其他字段,例如班级编号。课程名称....等 我可以使用表单字段来重命名上传文件吗?...附件是表单草稿

这是为了从我大学的工作人员上传数据。我已经处理了以前的建议,但仍然需要在表单字段上更改文件的名称。在此处输入图像描述

标签: google-apps-scriptgoogle-sheets-apigoogle-forms

解决方案


没有用于重命名上传文件的专用表单字段,但您可以创建一个附加字段,提示用户输入文件应重命名的名称。

如果上传文件,则其在 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)
}


推荐阅读