简单的一个:"创建文件夹"脚本将覆盖其先前的条目

时间:2016-05-21 08:01:06

标签: google-apps-script overwrite create-directory

我是一个新秀,所以请原谅我,如果这是一个' 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

1 个答案:

答案 0 :(得分:0)

在收到Sandy的输入后, 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 } } 命令触发了一个循环,我删除了它并修改了代码,只关注最后一行。现在,结合On Form Submit触发器,每次表单进入时,都会创建一个文件夹以及该文件夹的URL。修改后的代码:

{{1}}