0

I am trying to change ownership of files in Google Drive, where my service account isn't owner of the file.

 function getDriveFiles(folder, path) {
   var folder = DriveApp.getFolderById("0B23heXhtbThYaWdxzMc");
   var path = "";
    var files = [];
    var fileIt = folder.getFiles();
    while ( fileIt.hasNext() ) {
        var f = fileIt.next();
      if (f.getOwner().getEmail() != "[email protected]")
        files.push({owner: f.getOwner().getEmail(), id: f.getId()});
    }
    return files;
}

So my array looks like this:

  var files = [
     {[email protected], id=CjOqUeno3Yjd4VEFrYzg},
     {[email protected], id=CjOqUYWxWaVpTQ2tKc3c},
     {[email protected], id=CjOqUNTltdHo2NllkcWs},
     {[email protected], id=CjOqUVTRRMnU2Y0ZJYms},
     {[email protected], id=CjOqUXzBmeE1CT0VLNkE}, 
     {[email protected], id=CjfKj4ur7YcttORkXTn8D2rvGE},
     {[email protected], id=CjOqUY3RFUFlScDBlclk}
    ]

Next function that i need to pass this array to is batchPermissionChange which will batch change the ownership to my service account. However i would like it to run batchPermissionChange per user. So if e.g [email protected] have 4 files, i don't want the batchPermissionChange function to be triggered 4 times, i would like it to trigger it one time with [email protected], and include his four fileID's.

function batchPermissionChange(ownerEmail, filesArray){
Do batch job Google... https://www.googleapis.com/batch
}

Question

How do i run the function batchPermissionChange(ownerEmail, filesArray) with for e.g [email protected] with his 4 fileId's? I could loop through the array, like, 'for each item in array run batchPermissionChange', but that will trigger the batch-function 4 times for the user [email protected].

2
  • so what is the issue? What are you having trouble with? You don't actually ask a question here. Commented Mar 24, 2016 at 17:40
  • Updated the question. Commented Mar 24, 2016 at 17:54

1 Answer 1

1

When you retrieve the list of files, instead of pushing all the files into a single array, you can create a map of arrays, with the keys in the map being the owners, and the arrays being the list of files for that owner.

function getDriveFiles(folder, path) {
  var folder = DriveApp.getFolderById("0B23heXhtbThYaWdxzMc");
  var path = "";
  var files = {};
  var fileIt = folder.getFiles();
  while (fileIt.hasNext()) {
    var f = fileIt.next();
    var owner = f.getOwner().getEmail();
    var id = f.getId();

    if (owner != "[email protected]") {
      // if the owner doesn't exist yet, add an empty array
      if (!files[owner]) {
        files[owner] = [];
      }

      // push the file to the owner's array
      files[owner].push(id);
    }
  }
  return files;
}

The files object will end up looking something like this:

{
  '[email protected]': ['CjOqUeno3Yjd4VEFrYzg', 'CjOqUYWxWaVpTQ2tKc3c', 'CjOqUNTltdHo2NllkcWs', 'CjOqUVTRRMnU2Y0ZJYms'],
  '[email protected]': ['CjOqUXzBmeE1CT0VLNkE'],
  '[email protected]': ['CjfKj4ur7YcttORkXTn8D2rvGE', 'CjOqUY3RFUFlScDBlclk']
}

Now, in the area of your code where you want to call batchPermissionChange, do it like this:

for(var ownerEmail in files) {
  if(files.hasOwnProperty(ownerEmail)) {
    // NOTE: I'm not sure what the first parameter should be for this, but 
    // this shows how to send the array of files for just one user at a 
    // time, so change the first parameter if I got it wrong.
    batchPermissionChange(ownerEmail, files[ownerEmail]);
  }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Exactly something like this is what i'm looking for. I tried your suggestion, but the array is looking the same it did before. Should the array look the same or should i be able to see any visual changes?
It should be very different. I updated my answer with an example.
I see, maybe the last part where i send the array of files is wrong, I'm not quite sure with the "var owner;" and "ownerEmail".
oops, I fixed the var owner part. I had made a mistake there. It should make sense now.
Ah stupid me! I thought that the instructions you provided me where super clear, by the book this should work. I was targeting wrong function when testing. Yes now the array look's different (as it should). Will try to pass it through to the batch function now. Brb :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.