【问题标题】:Can I use a form field to rename upload pdf or doc using googlescript?我可以使用表单字段使用谷歌脚本重命名上传的 pdf 或 doc 吗?
【发布时间】:2019-10-07 18:01:37
【问题描述】:
我正在制作一个上传 pdf 或 doc 格式文件的表单。此表单具有其他字段,例如班级编号。课程名称....等我可以使用表单字段来重命名上传文件吗?...附件是表单草稿
这是用于上传我大学教职员工的数据。我已经处理了以前的建议,但仍需要在表单字段中更改文件名。enter image description here
【问题讨论】:
标签:
google-apps-script
google-sheets-api
google-forms
【解决方案1】:
没有用于重命名上传文件的专用表单字段,但您可以创建一个附加字段,提示用户输入文件重命名的名称。
如果上传文件,其在 Google 驱动器上的 URL 将插入到目标电子表格的相应字段中:
基于我提供给您的先前解决方案,这里是一个修改,它将文件重命名为相应表单字段中输入的名称:
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)
}
【解决方案2】:
亲爱的,
我在 stackoverflow 的人员 cmets 和有用的代码的帮助下完成了脚本,并进行了一些修改。我粘贴下面的代码。谢谢
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 CourseSubject=sheet.getRange(lastRow,3).getValue();
var CourseCode=sheet.getRange(lastRow,4).getValue();
var CourseNo=sheet.getRange(lastRow,5).getValue();
var ClassCode=sheet.getRange(lastRow,6).getValue();
var Staff=sheet.getRange(lastRow,7).getValue();
var url=sheet.getRange(lastRow,8).getValue();
var newResponse=CourseCode.toString()+CourseNo.toString()+"-"+ClassCode.toString();
sheet.getRange(lastRow,9).setValue(newResponse);
var secodarySheetId;
switch(Department) {
case "Chemistry":
secodarySheetId="1YrkaAil_tJK1WKnhzAiDs6vntqAWFvxL";//Paste here the Id of the destinationsheet in the Physics folder
break;
case "Physics":
secodarySheetId="168b6JIltI03EmOgV55984WfxJ5vhe49m";//Paste here the Id of the destinationsheet in the Physics folder
break;
case "Math":
secodarySheetId="1Zq32ttSz_LrXVTiQkXGIegBEc0v1f5uY";//Paste here the Id of the destinationsheet in the Physics folder
break;
case "Computer Science":
secodarySheetId="1QeWtEBtw1iI9IKZeC5-aXgifNV-2GgVD";//Paste here the Id of the destinationsheet in the Physics folder
break;
case "Biology":
secodarySheetId="1bkwjWR-WkHGc3j3LxuR_pJ7wLP8fb0SV";//Paste here the Id of the destinationsheet in the Physics folder
break;
}
var rowContents=sheet.getRange(lastRow,1,1,sheet.getLastColumn()).getValues();
// var dataFolder="1fQKYII5FpRQRbaYdoUUa3b2rjesr2K_4nHDQJzg22uk"
//create a new folder with a specific nane, say newResponse
// var parentFolder=DriveApp.getFolderById(secodarySheetId);
// var newFolder=parentFolder.createFolder(newResponse)
// Logger.log(getFileName('13NeENGwL9FWtMYnLV9r5P8kbqxjlNo8d'));
Logger.log(getFileName(secodarySheetId));
// Department folders
var DepartmentFolder = getFileName(secodarySheetId);
// Change file name
var MainFolder = DriveApp.getFolderById("0ByV7HytJeZ-5fkota3lRQ3llbGczb2lnMmV4dXN6cTZWa2VSZVljOWkzVDlGb2w0MHlBdUE"); // change accordingly to folder ID
var files = MainFolder.getFiles();
// new name of csv that I want to rename the file within my google drive folder
while(files.hasNext())
{ // iterate throught the csv files available
var file = files.next()
// will rename all csv's to processed
file.setName(newResponse);
}
DriveApp.getFolderById(secodarySheetId).addFile(file);
MainFolder.removeFile(file);
}
【解决方案3】:
我今天遇到了同样的问题并遇到了这个问题。
我对其进行了调整以自动遍历所有行以及一些变量以使其更易于自定义(选择名称列和 URL 列)。
它具有重命名“所有”记录的功能(当您重命名现有表单的记录时很有用我所做的 :) 或只是“最后一个”记录。最后一个很适合与 OnFormSubmitted 触发器(以“last”函数为目标)配对,以避免每次都遍历所有记录,您可以阅读更多关于它的信息here。
感谢您分享您的代码,它真的帮助了我!
如果有人需要,这里是代码:
var sheet=ss.getActiveSheet();
var rangeData = sheet.getDataRange();
var lastColumn = rangeData.getLastColumn();
var lastRow = rangeData.getLastRow();
// Notice the second 2, this is to avoid the Timestamp Column
var searchRange = sheet.getRange(2,2, lastRow-1, lastColumn-1);
// Replace with your values (Column A=1, B=2, etc...)
var nameColumn = 2; // B
var urlColumn = 5; // E
// Calculating index for array
nameColumn -= 2;
urlColumn -= 2;
// Use this to rename the last record
function last() {
var lastRowContents=sheet.getRange(lastRow,2,1,sheet.getLastColumn()).getValues()[0];
rename(lastRowContents);
}
// Use this to rename all records
function all() {
// Put rows in an array
var rangeValues = searchRange.getValues();
// Loop through the rows and rename file
for ( i = 0 ; i < lastRow - 1; i++){
row = rangeValues[i];
rename(row);
};
}
// Retrieves the ID and Name fields from the row, then
// renames the file
function rename(row) {
// Using the first field, Name (Index 0 becuse of the array, calculated above)
// ** Even though the Name field is the second column, we see it as the first one since
// we ignored the timestamp column in the searchRange **
var userName = row[nameColumn];
var url = row[urlColumn];
// Retrieve the ID from the URL
var Id = url.split('=')[1];
// Adapt this newFileName to your needs
var newFileName = userName;
// Get the file and rename it
DriveApp.getFileById(Id).setName(newFileName);
Logger.log("Renamed file with ID " + Id + " to " + newFileName);
};