2

due to some File migration from a Fileserver to Google Drive for a bunch of users in a company, it is necessary that I change the ownership of a Folder and all its containing subfolders and Files. First I copy the whole folder to my drive(get the ownership this way), then I start my script to transfer the ownership to the new owner.

I wrote a small Script that is working fine for some smaller Folders (<1GB approx.) but almost always gives me an Error when transferring ownership of bigger ones:

Action not allowed (Line 85, File"Code")

Line 85 is containing the following code:

file.setOwner(newOwner);

I have to run that script a few times, sooner or later it finishes successfully.

Have no clue why the script gets that error.

Any Ideas? Many thanks!

The script:

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Transfer')
      .addItem('Transfer Folder', 'Transferfolder')
      .addSeparator()
      .addItem('Disable Sharing Settings for Editors', 'Ownersettings')
      .addToUi();
}


function Transferfolder()
{
  var me=Session.getActiveUser().getEmail();
  var ss = SpreadsheetApp.getActive();
  var data = ss.getDataRange().getValues();
  var idname=data[2][1];  
  var newOwner=data[3][1];
  var fol = DriveApp.getFolderById(idname); 
   subfolderTransfer(fol,me,newOwner);
}
function Ownersettings()
{
  var me=Session.getActiveUser().getEmail();


  var ss = SpreadsheetApp.getActive();
  var data = ss.getDataRange().getValues();
  var idname=data[2][1];  
  var fol = DriveApp.getFolderById(idname);

  disablesharebyeditors(fol,me);
}
function disablesharebyeditors(fol,me)
{

  var files = fol.getFiles();
  var folders=fol.getFolders();


   Logger.log(fol.getOwner().getEmail());
  if(fol.getOwner().getEmail()==me){
  fol.setShareableByEditors(false);
  Logger.log(fol);
  }


  while (files.hasNext())
  {
    var file = files.next();

    if(file.getOwner().getEmail()==me){
    file.setShareableByEditors(false);
    }
  }
  while (folders.hasNext())
  {

    var subfolder = folders.next();

    disablesharebyeditors(subfolder,me);
  }
}

function subfolderTransfer(fol,me,newOwner)
{
 Logger.log("subfolderTransfer");
  var files = fol.searchFiles('"me" in owners');
  var folders=fol.searchFolders('"me" in owners')


  Logger.log(fol.getName());


  if(fol.getOwner().getEmail()==me){
  fol.setOwner(newOwner);

  }
   //file.removeEditor(me);

  while (files.hasNext())
  {
    var file = files.next();
     Logger.log(file.getName());
   if(file.getOwner().getEmail()==me){
    file.setOwner(newOwner);
   }
  }
 Logger.log("folders");
  while (folders.hasNext())
  {Logger.log(fol.getName());

    var subfolder = folders.next();

    subfolderTransfer(subfolder,me,newOwner);
  }
}


function transferfiles(files,me,newOwner)
{

  while (files.hasNext())
  {
    var file = files.next();

    if(file.getOwner().getEmail()==me){
    file.setOwner(newOwner);
    }
    //file.removeEditor(me);
  }
}
tehhowch
  • 9,645
  • 4
  • 24
  • 42
flunterWay
  • 21
  • 3
  • i suspect it's a rate limit. Try slowing down the requests and also make the script resumeable by noting which permissions have already been set. – pinoyyid Jul 20 '18 at 10:14
  • 1
    [Related](https://stackoverflow.com/questions/32746412/file-setowner-returns-action-not-allowed-when-transferring-ownership-to-an-e) – Adelin Jul 20 '18 at 11:28
  • I tried a Utilities.sleep(500); but it terminates the same way it does before. The current folder I try to handle is about 3,5GB containing about 7.5k Files. How could rate limit matter with this size? – flunterWay Jul 20 '18 at 12:21
  • You want to modify the permissions of a lot of files in your Drive. And the files have already been in your Google Drive. If my understanding is correct, how about using [the batch request](https://developers.google.com/drive/api/v3/batch)? The batch request can use 100 API calls by only one API call, and also it works by the asynchronous processing. So the process cost and speed will be lower and faster. By this, I thought that your issue might be resolved. If this was not what you want, I'm sorry. – Tanaike Jul 21 '18 at 02:03
  • yes this is what I want, the solution sounds very fruitful and I tried to get the python batch request to work with the code at the end of your link you provided but it just shows an error "NameError: name 'drive_service' is not defined" and I don't know how to define the drive service. Unfortunately, there is no further info or detailed documentation for this google-API-python-client. – flunterWay Jul 23 '18 at 13:25
  • You want to change from Google Apps Script to Python for achieving what you want. If my understanding is correct, can you update your question or post it as a new question? Because from your replying comment, I'm not sure about your current situation including the script. I'm really sorry for my poor skill. – Tanaike Jul 23 '18 at 22:50
  • do not use batch!!!!!! If you are rate limited, sleeping for 0.5 sec is not enough. You'll need to sleep for nearer to 2 secs. I suggest try a 3 second delay and see if the problem is resolved. If so, you can then refine your code to optimise the delay and retry any failures. – pinoyyid Jul 24 '18 at 20:46
  • I modified my code, gave it a sleep of 4000ms, but get the problem now that it aborts after 6 Minutes because of the well known "execution limit" problem that comes with google-scirpt. So the task is now to wrap my code into a loop, stop after 5 minutes and restart it again on the position where it stopped. Any ideas how to accomplish that with my code? – flunterWay Aug 01 '18 at 08:06

0 Answers0