The Folder ID is stored directly in the spreadsheet rather than extracting from the Folder Link via regex (example here) just for simplicity purposes. A confirmatory Yes is placed in the Permissions Added? column to help verify success as the script loops through each row.
There are lots of comments in the code to explain what each line is doing and most of the features have been covered elsewhere in the blog. Remember to add your main Google Drive folder ID into the config sheet where all the sub-folders will be created under.
Note there is now an updated/improved version of this tool detailed here.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function bulkCreateFolders() { | |
// get current spreadsheet | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
// get root Folder Id from 'config' sheet | |
var configSheet = ss.getSheetByName('config'); | |
var rootFolderId = configSheet.getRange(1, 2).getValue(); | |
Logger.log('Root folder id is: ' + rootFolderId); | |
// get array of data from 'data' sheet and the last row | |
var dataSheet = ss.getSheetByName('data'); | |
var data = dataSheet.getDataRange().getValues(); | |
var lastRow = dataSheet.getLastRow(); | |
// check a specific value from the collected data | |
Logger.log('First marker is: ' + data[1][1]); | |
// start of loop to go through each row in turn ******************************** | |
for (var i=1; i<lastRow; i++) { | |
// extract values from row of data so easier to work with | |
var moduleName = data[i][0]; | |
var marker = data[i][1]; | |
var markerEmail = data[i][2]; | |
// create a name for the new Folder | |
var folderName = moduleName + ' - ' + marker; | |
Logger.log('Folder name is: ' + folderName); | |
// create new Drive Folder | |
var newFolder = DriveApp.getFolderById(rootFolderId).createFolder(folderName); | |
// get new Drive Folder Id for later use | |
var newFolderId = newFolder.getId(); | |
// set Folder ID cell | |
dataSheet.getRange(i+1, 4).setValue(newFolderId); | |
// set Folder Link cell using Drive Folder Id | |
dataSheet.getRange(i+1, 5).setFormula('=HYPERLINK("https://drive.google.com/drive/folders/' | |
+ newFolderId + '")'); | |
// add Marker permission using email address | |
newFolder.addEditor(markerEmail); | |
// write confirmation into 'Permission Added?' cell | |
dataSheet.getRange(i+1, 6).setValue('Yes'); | |
// create sub-folder ********************************** | |
var subFolderName = moduleName + ' - ' + 'Marksheets'; | |
var newSubFolder = newFolder.createFolder(subFolderName); | |
// end of create sub-folder *************************** | |
}// end of loop to go through each row in turn ********************************** | |
} |
its working great! Thanks.
ReplyDeleteThis saved me tons of time.
Excellent! Thanks for the feedback :)
DeleteHow do I get this to loop from the latest added row?
ReplyDeleteHi
Deleteso you mean you run it once, add some more rows, and want it to be able to re-run just for those?
I assume you just delete the orginal rows, add new ones and hit run?
DeleteHi
DeleteIn the example above it was designed to by run once only and then cleared for the next iteration. But you could add an 'if' statement to it which checked that the 'Folder ID' column was blank before proceeding for example, meaning it could be re-run safely.
Something like this inserted at Line 22 would then only run the rest of the code if true:
if (data[i][4] == '') {
// rest of code goes in here ...
}
Thanks
I had to use if(data[i][4]==null) not if (data[i][4] == '') because my blank spreadsheet cells were undefined not set to an empty string!
DeleteOn a different sheet, however, I had to use if (data[i][4] == '') and the data[i][4]==null didn't work. Any ideas why that might be?
DeleteHi
DeleteI'm afraid not - they should be completely empty by default (as you found when creating a new sheet) - it might be worth starting afresh on the new sheet.
Bravo, this worked like a charm. Keep up the great work!!!
ReplyDeleteThank you!
DeleteI don't understand getting the root folder ID from 'config sheet.' I tried to make sense of this and run my code but ran into an error. Any help would be great appreciated! Thank you.
ReplyDeleteHi
DeleteI implement that because at my place of work non-coders are taking ownership of these spreadsheets/scripts and so I need to make them universally accessible without risking people going into the Apps Script code itself.
By putting such values into a spreadsheet they can be easily updated by anybody that wishes to use it.
I don't understand getting the root folder ID from 'config sheet.' I tried to make sense of this and run my code but ran into an error. Any help would be great appreciated! Thank you.
ReplyDeleteResolved. You should make a sheet on your spreadsheet and label it config, and in row 1 column 2 put the rootID of the folder you want all your sub-folders to go in. this can be found by going to that folder in drive and copying everything after the last slash (/)
DeleteThank you greatly for making this script. I just used it successfully create a new directory of folders. It was a huge help.
ReplyDeleteits not working for me. It starts and directly ended without any error massage
ReplyDeleteHi
DeletePlease can I recommend you try the latest version of this tool here: https://www.pbainbridge.co.uk/2020/06/bulk-create-google-drive-folders-20.html
Kind regards
Phil