Assign a new owner to Folder(s) in Google Drive with Apps Script

Ames Computer Geeks Corner News Assign a new owner to Folder(s) in Google Drive with Apps Script NYC New York City North Bergen County

Working with Google Drive one quickly realizes there are many good reasons to automate many tasks. Simple tasks, like creating, gathering and displaying folders is more than tedious and time consuming. Here we utilize a function from another blog 'Search for folders and display' to search for folders and permission users as Editors in my project team.

This functionality is built on top of previous blogs such as, 'Create a Folder in the Root Drive' and 'Iterate and Display folder's full path'.


I often work with data sets that are populated from a database or another spreadsheet to analyze big chunks of data. This requires creation of folders and files on demand that will hold my spreadsheets and data. The folders and files are created and manipulated dynamically; therefore the data used needs to be cleaned up and removed from my Google Drive when the process has completed. Once I have my spreadsheet built out from my datasets, I automate many of the tasks using my Google Apps Script code base. In our example we will employ the DriveApp Class to manage, create and find files and folders in Google Drive. Since we are working primarily in folder context, we will need to manipulate folders and listing files using Folder Class, which creates, deletes and manages folders and files.

Just like all teams working on projects some team members require read only permissions others require editing rights. If you are looking for read only viewer permissions check out my other blog: Add Viewers Permission to Folder(s)

In this blog we will focus on adding editors that require read/write access to my work folders, and then transferring the ownership of the folder to one of the editors. We will also cover removing an editor from being able to read/write to a folder.

To start, we must first search for our parent folder. The DriveApp Class function allows us to narrow down a specific set of folders using a name search: 'DriveApp.getFoldersByName("folder-string-name")'. This function returns a 'FolderIterator' object that we will use to iterate over found folders and assign editors permission to our team members. Let's begin with a helper function to create and return a list of users using their email addresses.


function crtEditorList()


We proceed with the next function that will accept two parameters: first, 'emailList' array returned from the 'crtEditorList()' function; second, 'folderObj' returned from calling the function 'DriveApp.getFoldersByName("test01")'.


function addEditorListToFolder(emailList, folderObj)


Since 'DriveApp.getFoldersByName("test01")' returns a folder iterator object, even if it is a single folder returned, we must construct a loop to iterate the returned 'FolderIterator' class.

At this point we have the new list of editors assigned to my folder. The following two functions, first of which will remove one of the editors and the second will transfer ownership from myself to a new editor.


  delEditorFromFolder("valid-email-02@gmail.com", folderObj);
  assignNewOwner("valid-email-01@gmail.com", folderObj);


Both of the functions require two parameters: first parameter is an email address and second, the folder object we are working with. Once the ownership is transferred to another user, you can not take the ownership away. The new owner becomes the primary user that controls the folder, and must transfer the ownership back to you manually.

Putting it all together:


function assignNewOwner(emailAddr, folderObj) {
  folderObj.setOwner(emailAddr);
};

function delEditorFromFolder(emailAddr, folderObj) {
  folderObj.removeEditor(emailAddr);
};

function addEditorListToFolder(emailList, folderObj) {
  folderObj.addEditors(emailList);
};

function crtEditorList() {
  var emailList = [];
  emailList.push("valid-email-01@gmail.com");
  emailList.push("valid-email-02@gmail.com");

  return(emailList);
};

function myMainBlog01() {
  var folderObj = null;
  var folderNameIter = DriveApp.getFoldersByName("test01");
  var emailList = crtEditorList();

  if(folderNameIter.hasNext()){
    while( folderNameIter.hasNext() ) {
      folderObj = folderNameIter.next();
      addEditorListToFolder(emailList, folderObj);
    };
  };   
  
  delEditorFromFolder("valid-email-02@gmail.com", folderObj);
  assignNewOwner("valid-email-01@gmail.com", folderObj);
  
};