1

So I'd like to create and (publicly) share a new folder within an existing parent.

Eg: \September 2013\[new folder here]

Sure, you could:

  1. Use createFolder, to create a folder in root
  2. Use addToFolder, to copy newly created folder into a given parent
  3. Use removeFromFolder, to remove the folder from root

But then, publicly sharing that folder is not possible!

Indeed, if you try using: setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.EDIT);

You will get: "TypeError: Cannot find function setSharing in object Folder."

That's right, the above DriveApp function seems to only work with folders made with DriveApp.createFolder.

And of course, there is no way to simply:

  1. Use DriveApp.createFolder, to create a folder in root
  2. Use setSharing, to publicly share the newly created folder
  3. Move the new folder to the desired sub-folder

... as there is no move method!

Has anyone found a solution to such a problem?

4

2 回答 2

3

You don't need to use 2 services, driveApp has all you need already build in

You simply forgot that you can create a folder from another one, not only from root... so you don't need to move anything.

example :

function createSharedFolder() {
  var parent = DriveApp.createFolder('testFolder');
  var child = parent.createFolder('child');
  child.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.EDIT);
}

To solve these simple questions think about making use of the autocomplete, see screen capture below :

enter image description here

if your parent folder already exist or if your not sure about it, here is a function that handles all use cases : (+ a function to test the function)

function test(){
 createSharedSubFolder('Testparent','Testchild2');
}

function createSharedSubFolder(parent,child) { // folder names as string parameters
  var folders = DriveApp.getFolders();
  var exist = false
  while (folders.hasNext()) {
    var folder = folders.next();
    if(folder.getName()==parent){exist = true ; var folderId = folder.getId() ; break};// find the existing folder
 }
  if(exist){
  var child = DriveApp.getFolderById(folderId).createFolder(child).setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.EDIT);
  }else{
  var childFolder = DriveApp.createFolder(parent).createFolder(child);
  childFolder.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.EDIT);
  } 
}
于 2013-08-04T16:27:41.183 回答
2

A combination of DocsList and DriveApp can be useful.

/* CODE FOR DEMONSTRATION PURPOSES */
function main() {
  var parentFolder = DocsList.getFolder('September 2013');
  var idNewFolder = parentFolder.createFolder('New Folder').getId();
  var newFolder = DriveApp.getFolderById(idNewFolder);
  newFolder.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.EDIT);
}
于 2013-08-04T15:41:13.097 回答