Would it be possible to use an array under 'value' here to prevent me from creating a group alias email address? For example:
userValues = ["[email protected]", "[email protected]", "[email protected]"];
Drive.Permissions.insert({
'role': 'writer',
'type': 'user',
'value': ** userValues ** ,
},
folder, {
'sendNotificationEmails': 'false'
});
Here is my understanding:
userValues = ["[email protected]", "[email protected]", "[email protected]"];In the current stage, Drive.Permissions.insert() can create the permission for one email. Unfortunately, the permission cannot be created with the multiple email by one call of Drive.Permissions.insert(). If you want to use the array and Drive.Permissions.insert, in the current stage, it is required to run Drive.Permissions.insert in the for loop.
As a workaround, here, I would like to propose to use the batch request. When the batch request is used, 100 API calls can be done by one API call and it can be run with the asynchronous process.
In this pattern, the batch request is run with UrlFetchApp.
Before you run the script, please set the file ID and email addresses. If you want to add the permission to the folder, please set the folder ID to ### of const fileId = "###";.
function myFunction() {
const fileId = "###"; // Please set the file ID.
const userValues = ["[email protected]", "[email protected]", "[email protected]"]; // 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())
}
In this pattern, a Google Apps Script library for the batch request is used.
Before you run the script, please set the file ID and email addresses, and please install the GAS library.
function myFunction() {
const fileId = "###"; // Please set the file ID.
const userValues = ["[email protected]", "[email protected]", "[email protected]"]; // 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())
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With