我是一个彻头彻尾的菜鸟,所以如果这是一个'duh',请原谅我。
我修改了一个脚本,以便为每个提交的表单创建一个新的 Gdrive 文件夹。问题是,每次脚本运行时,它都会为电子表格中的每一行重新创建一个文件夹。我错过了什么?
function folderMaker(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form Responses 1');
var last = sheet.getLastRow();//end of roster size
Logger.log('last '+last);
for(var i=2;i<last+1;i++){
var TimeStamp = sheet.getRange(i,1).getValue();//get timestamp code
var eLastName = sheet.getRange(i,39).getValue();//get Employee's Last NameID
var eFirstName = sheet.getRange(i,40).getValue();//get Employee's Last NameID
var eID = sheet.getRange(i,38).getValue();//get Employee's Last NameID
var folder = DriveApp.createFolder(eLastName + ' ' + eFirstName + ' - ' + eID + ' ('+TimeStamp + ')' );
var aFolder = folder.getName();
var aFolderId = folder.getId();
var aFolderUrl = folder.getUrl();
sheet.getRange(i, 82).setValue(aFolder);
sheet.getRange(i, 83).setValue(aFolderId);
sheet.getRange(i, 84).setValue(aFolderUrl);
//Add analyst folders to HRC folder
var HRCFolderID = '[insert form id]'
var HRCFolder = DriveApp.getFolderById(HRCFolderID);
var analystFolder = DriveApp.getFolderById(aFolderId);
HRCFolder.addFolder(analystFolder);//put analyst folder in HRC folder
DriveApp.getRootFolder().removeFolder(analystFolder);//take analyst folder out of Google Drive
在收到 Sandy 的输入后,for
命令触发循环,我将其删除并修改代码以仅关注最后一行。现在,结合"表单提交"触发器,每次进入表单时,都会创建一个文件夹以及该文件夹的 URL。修订后的代码如下:
function folderMaker(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form Responses 1');
var last = sheet.getLastRow();//end of roster size
Logger.log('last '+last);{
var TimeStamp = sheet.getRange(last,1).getValue();//get timestamp code
var eLastName = sheet.getRange(last,39).getValue();//get Employee's Last NameID
var eFirstName = sheet.getRange(last,40).getValue();//get Employee's Last NameID
var eID = sheet.getRange(last,38).getValue();//get Employee's Last NameID
var folder = DriveApp.createFolder(eLastName + ' ' + eFirstName + ' - ' + eID + ' ('+TimeStamp + ')' );
var aFolder = folder.getName();
var aFolderId = folder.getId();
var aFolderUrl = folder.getUrl();
sheet.getRange(last, 82).setValue(aFolder);
sheet.getRange(last, 83).setValue(aFolderId);
sheet.getRange(last, 84).setValue(aFolderUrl);
//Add analyst folders to HRC folder
var HRCFolderID = '[insert form id]'
var HRCFolder = DriveApp.getFolderById(HRCFolderID);
var analystFolder = DriveApp.getFolderById(aFolderId);
HRCFolder.addFolder(analystFolder);//put analyst folder in HRC folder
DriveApp.getRootFolder().removeFolder(analystFolder);//take analyst folder out of Google Drive
}
}