数组传递给另一个函数 [英] Pass array to another function

查看:157
本文介绍了数组传递给另一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图改变文件的所有权在谷歌驱动器,在我的服务帐户不是文件的所有者。

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() != "service@domain.com")
        files.push({owner: f.getOwner().getEmail(), id: f.getId()});
    }
    return files;
}

所以,我的阵列看起来像这样:

So my array looks like this:

  var files = [
     {owner=jens@domain.com, id=CjOqUeno3Yjd4VEFrYzg},
     {owner=jens@domain.com, id=CjOqUYWxWaVpTQ2tKc3c},
     {owner=jens@domain.com, id=CjOqUNTltdHo2NllkcWs},
     {owner=jens@domain.com, id=CjOqUVTRRMnU2Y0ZJYms},
     {owner=jack@domain.com, id=CjOqUXzBmeE1CT0VLNkE}, 
     {owner=aurora@domain.com, id=CjfKj4ur7YcttORkXTn8D2rvGE},
     {owner=aurora@domain.com, id=CjOqUY3RFUFlScDBlclk}
    ]

Next函数,我需要通过这个数组是batchPermissionChange将批量文件的所有权改为我的服务帐户。但是我想它运行每个用户batchPermissionChange。因此,如果如jens@domain.com有4个文件,我不想batchPermissionChange功能被触发4次,我想它与jens@domain.com触发它一次,包括他的四个FILEID的。

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 jens@domain.com 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 jens@domain.com, and include his four fileID's.

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

我如何运行功能batchPermissionChange(OWNEREMAIL,filesArray)与例如jens@domain.com与他的4 FILEID的?我可以遍历数组一样,'在阵列中的每个项目上运行batchPermissionChange,但将触发批量功能的4倍,为用户jens@domain.com。

How do i run the function batchPermissionChange(ownerEmail, filesArray) with for e.g jens@domain.com 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 jens@domain.com.

推荐答案

在检索文件的列表,而不是推的所有文件到一个单一的阵列,可以创建地图阵列,与地图的钥匙作为业主,以及阵列是文件列表中为所有者。

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 != "service@domain.com") {
      // 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:

{
  'jens@domain.com': ['CjOqUeno3Yjd4VEFrYzg', 'CjOqUYWxWaVpTQ2tKc3c', 'CjOqUNTltdHo2NllkcWs', 'CjOqUVTRRMnU2Y0ZJYms'],
  'jack@domain.com': ['CjOqUXzBmeE1CT0VLNkE'],
  'aurora@domain.com': ['CjfKj4ur7YcttORkXTn8D2rvGE', 'CjOqUY3RFUFlScDBlclk']
}

现在,在您要拨打 batchPermissionChange 您code的地区,像这样做:

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

这篇关于数组传递给另一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆