0

I wondered if someone could help. We use Google Apps for Education. Within the system we have a shared folder where teachers can place files for students to access. E.g. The entire team of 8 science teachers all add files to "Science Shared."

Science Shared folder is on a separate google account "science-shared@domain.com"

Over time, files will take up quota of individual users and if they leave and their account is deleted, all these files will go. We obviously do not want to transfer their entire data to science-shared using the transfer facility.

Ideally, I am looking for some sort of script which can traverse through the shared folder and change the permissions of each file and folder so that science-shared is the owner and the individual teacher has edit access.

Is this possible and if so, can anyone provide some help on how/where to start...clueless at the moment.

Thanks in advance.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
user3833053
  • 3
  • 1
  • 2

1 Answers1

0

Edit:

Refer to issue 2756, noting that an administrator cannot change the ownership of files via Google Apps Script:

...It's related to the fact that you can't change the own for files you don't own. This error occurs for any illegal ACL change, such as trying to call addEditor() when you are a viewer.

To change the ownership of files not owned by the administrator, they must use the Google Drive SDK, authenticated via OAuth.


This is certainly possible, although only for files owned by the user running the script.

Here's a script that will find all the files owned by the current user in the Science Shared folder, and transfer ownership to user science-shared. It's designed as a spreadsheet-contained script, which creates a custom menu and uses the spreadsheet Browser UI. Put the spreadsheet into your shared directory, and any teacher should be able to use it to transfer their own files, wholesale.

An admin should be able to use the script to change ANY teacher's files - just collect the id of the origOwner, and pass it to chownFilesInFolder.

Caveat: It only deals with files, not sub-directories - you could extend it if needed.

/**
 * Find all files owned by current user in given folder, 
 * and change their ownership to newOwner.
 * Note: sub-folders are untouched
 */
function chownFilesInFolder(folderId,origOwner,newOwner) {
  var folder = DriveApp.getFolderById(folderId);
  var contents = folder.getFiles();

  while(contents.hasNext()) {
    var file = contents.next();
    var name = file.getName();
    var owner = file.getOwner().getEmail();
    // Note: domain security policies may block access to user's emails
    // If so, this will return a blank string - good enough for our purposes.

    if (owner == origOwner) {
      // Found a file owned by current user - change ownership
      Logger.log(name);
      //file.setOwner(newOwner);
    }   
  }
};

/**
 * Spreadsheet browser-based UI driver for chownFilesInFolder()
 */
function changeOwnership() {
  var resp = Browser.msgBox("Transfer 'Science Shared' files",
                            "Are you sure you want to transfer ownership of all your shared files?",
                            Browser.Buttons.YES_NO);

  if (resp == "yes") {
    var folderName = "Science Shared";
    // Assume there is just one "Science Shared" folder.
    var folder = DriveApp.getFoldersByName(folderName);
    if (!folder.hasNext()) {
      throw new Error("Folder not found ("+folderName+")");
    }
    else {
      var folderId = folder.next().getId();
      var origOwner = Session.getActiveUser().getEmail();  // Operate on own files
      var newOwner = "science_shared@example.com";

      chownFilesInFolder(folderId,origOwner,newOwner);

      Browser.msgBox("Operation completed", Browser.Buttons.OK);
    }
  }
  else Browser.msgBox("Operation aborted", Browser.Buttons.OK);
}

/**
 * Adds a custom menu to the active spreadsheet, containing a single menu item
 */
function onOpen() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var entries = [{
    name : "Transfer 'Science Shared' files",
    functionName : "changeOwnership"
  }];
  spreadsheet.addMenu("Science-Shared", entries);
};

I hope that helps get you started.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • Hi Mogsdad, Thanks for this. I cannot seem to get it to work (pretty new to Google Scripts). The issue is, as you say - we would change this on behalf of teachers; traversing through the Shared folder (as an admin) and setting owner to scienceshared@... There are hundreds of sub-directories. I have tried getting the original owner with what I can find on Google's Developers site but it just seems to get errors of cannot find the function in FolderIterator. I also wouldn't know where to begin with sub-folders. Thank you SO much for everything so far, would you be able to help with the above? – user3833053 Jul 13 '14 at 09:48
  • Been playing with it this afternoon; I get so far and just get "Drive App Access denied" Any help? I am logged in as a superadmin within our Google Apps Domain. [14-07-13 18:44:48:061 BST] Execution failed: Access denied: DriveApp. – user3833053 Jul 13 '14 at 17:46
  • The 'cannot find function' problem was likely misuse of the FolderIterator - you need `folder = folderIter.next()` or similar, then `folder.getOwner()` should apply. No idea about the Access Denied problem - that's usually a permissions problem, which a superadmin should be immune from. Do you know what operation threw that? – Mogsdad Jul 13 '14 at 20:42
  • Will that work with each file within the folder? The operation that threw it was "file.setOwner(newOwner);" – user3833053 Jul 13 '14 at 21:45
  • I thought that it could be that Google Drive API was off in the Advanced Services - putting it on has no impact. – user3833053 Jul 13 '14 at 21:57
  • I wish I had better news, but it turns out that you just can't do that via Google Apps Script. See updated answer. – Mogsdad Jul 14 '14 at 01:20