是否可以在此处使用“值”下的数组来阻止我创建群组别名电子邮件地址?例如:
userValues = ["user1@abc.com", "user2@abc.com", "user3@abc.com"];
Drive.Permissions.insert({
'role': 'writer',
'type': 'user',
'value': ** userValues ** ,
},
folder, {
'sendNotificationEmails': 'false'
});
最佳答案
这是我的理解:
- 您想使用多个电子邮件地址授予文件和文件夹权限。
- 例如,您想要使用类似
userValues = ["user1@abc.com", "user2"@abc.com", "user3@abc.com"];
的数组
- 例如,您想要使用类似
- 您想使用 Google Apps 脚本实现这一目标。
问题和解决方法:
在当前阶段,Drive.Permissions.insert()
可以为一封电子邮件创建权限。不幸的是,无法通过一次调用 Drive.Permissions.insert()
使用多个电子邮件创建权限。 .如果你想使用数组和Drive.Permissions.insert
,在当前阶段,需要运行Drive.Permissions.insert
在 for 循环中。
作为解决方法,在这里,我想建议使用批量请求。使用批量请求时,1次API调用可完成100次API调用,可异步运行。
模式一:
在此模式中,批处理请求使用 UrlFetchApp 运行。
示例脚本:
在运行脚本之前,请设置文件 ID 和电子邮件地址。如果要给文件夹添加权限,请设置文件夹ID为###
的 const fileId = "###";
.
function myFunction() {
const fileId = "###"; // Please set the file ID.
const userValues = ["user1@abc.com", "user2"@abc.com", "user3@abc.com"]; // Please set the email addresses.
const resources = userValues.map(e => ({role: "writer", type: "user", emailAddress: e}));
const boundary = "xxxxxxxxxx";
const payload = resources.reduce((s, e, i) => {
s += "Content-Type: application/http\r\n" +
"Content-ID: " + i + "\r\n\r\n" +
"POST https://www.googleapis.com/drive/v3/files/" + fileId + "/permissions?sendNotificationEmails=false" + "\r\n" +
"Content-Type: application/json; charset=utf-8\r\n\r\n" +
JSON.stringify(e) + "\r\n" +
"--" + boundary + "\r\n";
return s;
}, "--" + boundary + "\r\n");
const params = {
method: "post",
contentType: "multipart/mixed; boundary=" + boundary,
payload: payload,
headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()},
};
const res = UrlFetchApp.fetch("https://www.googleapis.com/batch", params);
console.log(res.getContentText())
}
模式二:
在此模式中,使用了用于批处理请求的 Google Apps 脚本库。
示例脚本:
在运行脚本之前,请设置文件 ID 和电子邮件地址,以及 please install the GAS library.
function myFunction() {
const fileId = "###"; // Please set the file ID.
const userValues = ["user1@abc.com", "user2"@abc.com", "user3@abc.com"]; // Please set the email addresses.
const reqs = userValues.map(e => ({
method: "POST",
endpoint: "https://www.googleapis.com/drive/v3/files/" + fileId + "/permissions?sendNotificationEmails=false",
requestBody: {role: "writer", type: "user", emailAddress: e},
}));
const requests = {batchPath: "batch/drive/v3", requests: reqs};
const res = BatchRequest.Do(requests);
console.log(res.getContentText())
}
注意事项:
- 请在脚本编辑器中启用 V8。
- 在上面的脚本中,作为示例脚本,最大请求数为100。如果要请求超过100,请修改上面的脚本。请注意这一点。
引用资料:
关于google-apps-script - Drive.Permissions.insert (Value) - Drive API,你可以使用 'value' 下的数组吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60859530/