google-apps-script - Drive.Permissions.insert (Value) - Drive API,你可以在“value”下使用数组吗?
问题描述
是否可以在此处使用“值”下的数组来阻止我创建组别名电子邮件地址?例如:
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()
。如果要使用数组and Drive.Permissions.insert
,在当前阶段,需要Drive.Permissions.insert
在for循环中运行。
作为一种解决方法,在这里,我想建议使用批处理请求。使用批量请求时,一个API调用可以完成100个API调用,并且可以与异步进程一起运行。
模式一:
在此模式中,批处理请求使用 UrlFetchApp 运行。
示例脚本:
在运行脚本之前,请设置文件 ID 和电子邮件地址。如果要给文件夹添加权限,请将文件夹ID设置为###
of 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 和电子邮件地址,并安装 GAS 库。
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,请修改上述脚本。请注意这一点。
参考:
推荐阅读
- c++ - boost::asio::basic_waitable_timer `cancel` 行为
- python - 从嵌套字典中删除所有 None 值
- tensorflow - Keras 中使用 softmax 到 one-hot 的自定义损失
- ubuntu - 为什么 subversion 在没有任何信息的情况下挂起?
- react-native - 无法从 JSON 数组中提取任何属性
- php - 1 个文件检查,其他文件也检查,但必须在不更改变量的情况下连接
- c++ - FObjectFinder 访问冲突
- python - Python - 替换exec中的函数和其中导入的模块
- c# - 模拟函数返回其输入时的 TargetParameterCountException
- azure - 删除 ARM 模板数据工厂中的某些组件?