2
votes

Im using the script editor in a Google spreadsheet, and I want to create a folder inside a root folder , already created. I've tried to follow the official notice of the API DriveApp, but without success. Here is my code :

function createSubFolder(rootFolder,name){
  DriveApp.getFoldersByName(rootFolder);
  DriveApp.createFolder(name);
}

I've tried also ( following this old post working with the old version of DriveApp : i.e DocsList) :

function createSubFolder(rootFolder,name){
  DriveApp.getFoldersByName(rootFolder).createFolder(name);
}

It seems to be very easy but I don't see where am I wrong. Does anyone know the way to do it?

Thanks

1

1 Answers

3
votes

You need to assign values to a variable.

function createSubFolder(rootFolder,name) {
  var AllFoldersWithThisName = DriveApp.getFoldersByName(rootFolder);
  var theFirstFolderWithThisName = AllFoldersWithThisName.next();

  theFirstFolderWithThisName.createFolder(name);
};

In Google Drive, you could have 1,000 folders all with the same name. Same thing with file names.

The getFoldersByName() method returns:

FolderIterator — a collection of all folders in the user's Drive that have the given name

You need to get one of the folders out of the collection of folders. That is done with the next() method.