Search code examples
google-apps-scriptdirectoryparentsharingsubdirectory

Google script: Create and publicly share a folder within existing parent


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?


Solution

  • 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);
    }