Reputation: 21
I am trying to automate the creation of new folders in Google drive from data in a spreadsheet. The spreadsheet gets information from a form. I have figured out how to make the new folders using scripts but have the following questions:
I want to copy a spreadsheet from another location and insert in the new folders that I am creating. The new file name is to be created with information from the same spreadsheet.
function makeFolders(){
//Get array of folder names (fixed & variable)
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var bottomRow = sheet.getMaxRows()-2;
var folderNames = sheet.getRange(2, 12, bottomRow, 1).getValues();
//If fixed name cell is blank it throws up an alert and stops
if (folderNames === "") {
SpreadsheetApp.getUi().alert('Cell A3 is blank.');
}
//If folder ID cell is blank it throws up an alert and stops
var folderUrl = sheet.getRange(3,14).getValue();
if (folderUrl === "") {
SpreadsheetApp.getUi().alert('Cell C3 is blank.');
}
//If fixed name cell and file URL aren't empty, run the program
if (folderNames !== "" && folderUrl !== "") {
//Get ID from folder URL
var folderId = folderUrl.match(/[-\w]{25,}/);
//Get master folder
var getRootFolderId = DriveApp.getFolderById(folderId);
//Copy master n times and rename each copy with the new name
//stops when it comes to a blank cell
for (n=0;n<bottomRow;n++){
if(folderNames[n] == ""){break;}
var newFolder = getRootFolderId.createFolder(folderNames[n]);
}
ss.toast("Your folders have been made! :)","Finished",3);
}
}**strong text**
Upvotes: 2
Views: 1268
Reputation: 925
You could add a timestamp to the name of the new folder created. Each timestamp would be unique to the millesecond:
function uniqueFolderName() {
var folderName = "1808";
var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyyMMddHHmmssSSS");
var newFolderName = folderName + formattedDate;
Logger.log(newFolderName);
}
Upvotes: 1
Reputation: 813
How do I avoid creating duplicate folders each time the script is run? For example if a folder named '1808' has already been created, I don't want the script to create '1808(1)'
You could get a list of all the folder names and loop through them to check if any match the new folder name! Something like:
function MakeNewFolders(rootFolderID, folderNames)
// Get master folder
var rootFolder = DriveApp get folder (rootFolderID)
var subfolders = rootFolder.getFolders()
var alreadyExists = false;
var newFolder;
// Check if any subfolder already exists
// loop for folders first, to minimize GAS function calls
while (subfolders.hasNext()) {
var subfolderName = subfolders.next().getName();
loop (each folder in folderNames) {
if (subfolderName == newFolderName)
alreadyExists = true;
}
if (!alreadyExists)
newFolder = rootFolder.createFolder(newFolderName)
}
If there are a TON of folders you need to loop through (more than 60-ish), here is a code snippet you might find handy. Basically, the original Google example for getting a list of subfolders runs twice as slow as necessary by checking for folder.hasNext()
, when you can just use a try-catch block and ignore the "you've reached the end!" exception it throws. Should cut execution time in half! :)
I want to copy a spreadsheet from another location and insert in the new folders that I am creating. The new file name is to be created with information from the same spreadsheet.
Try something like this!
function CopyFile(originalFileID, destinationFolderID) {
var destinationFolder = DriveApp.getFolderById(destinationFolderID) ;
var fileToCopy = DriveApp.getFileById(originalFileID)
var copiedFile = fileToCopy.makeCopy(<new name>, destinationFolder)
fileToCopy.setTrashed(true); // alternatively, you can use
// Drive.Files.remove(originalFileID);
}
Hope this helps! Been a while since I used GAS, hopefully this compiles :)
Upvotes: 1