我正在尝试更改Google云端硬盘中文件的所有权,我的服务帐户不是该文件的所有者。
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;
}
所以我的数组看起来像这样:
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}
]
我需要传递此数组的下一个函数是batchPermissionChange,它将批量更改所有权到我的服务帐户。但是,我希望它为每个用户运行batchPermissionChange。因此,如果例如jens@domain.com有4个文件,我不希望batchPermissionChange函数被触发4次,我希望它用jens@domain.com触发一次,并包含他的四个fileID&# 39; S
function batchPermissionChange(ownerEmail, filesArray){
Do batch job Google... https://www.googleapis.com/batch
}
问题
如何使用例如jens@domain.com和他的4个fileId运行函数batchPermissionChange(ownerEmail,filesArray)?我可以遍历数组,例如,对于数组中的每个项目运行batchPermissionChange',但这将为用户jens@domain.com触发批处理功能4次。
答案 0 :(得分:1)
当您检索文件列表时,您可以创建一个数组映射,而不是将所有文件推送到一个数组中,映射中的键是所有者,而数组是该文件的列表。所有者。
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;
}
files
对象最终会看起来像这样:
{
'jens@domain.com': ['CjOqUeno3Yjd4VEFrYzg', 'CjOqUYWxWaVpTQ2tKc3c', 'CjOqUNTltdHo2NllkcWs', 'CjOqUVTRRMnU2Y0ZJYms'],
'jack@domain.com': ['CjOqUXzBmeE1CT0VLNkE'],
'aurora@domain.com': ['CjfKj4ur7YcttORkXTn8D2rvGE', 'CjOqUY3RFUFlScDBlclk']
}
现在,在您要调用batchPermissionChange
的代码区域中,请执行以下操作:
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]);
}
}