Title: Drive.Permissions.insert (Value) - Drive API, can you use an array under 'value'?
Tags: google-apps-script;google-drive-api
Question: Would it be possible to use an array under 'value' here to prevent me from creating a group alias email address? For example:
userValues = [&quot;john47@example.net&quot;, &quot;john47@example.net&quot;, &quot;john47@example.net&quot;];
Drive.Permissions.insert({
'role': 'writer',
'type': 'user',
'value': ** userValues ** ,
folder, {
'sendNotificationEmails': 'false'
Comment: In the current stage, at Drive.Permissions.insert, one email can be used for one call. So, unfortunately, such method cannot be used. As a workaround, how about using 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. [Ref](https://developers.google.com/drive/api/v3/batch) 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. If I misunderstood your question, I apologize.

Comment: If you use the batch request, for example, there is a GAS library for running the batch request. https://github.com/tanaikech/BatchRequest

Comment: I thought that when the sample script is shown, that might be useful for you and other users. So I proposed 2 sample scripts. Could you please confirm it? If that was not the direction you want, I apologize.

Comment: Thank you @Tanaike! I will look into this! So far, I only have to use this for two users but who knows, the list might get bigger. So looking for an alternative now incase it happens.

Here is the accepted answer: Here is my understanding:
You want to give the permission to a file and folder using multiple email addresses.
For example, you want to use an array like userValues = [&quot;john47@example.net&quot;, &quot;user2&quot;@abc.com&quot;, &quot;john47@example.net&quot;];
You want to achieve this using Google Apps Script.
Issue and workaround:
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.
Pattern 1:
In this pattern, the batch request is run with UrlFetchApp.
Sample script:
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 = &quot;###&quot;;.
function myFunction() {
const fileId = &quot;###&quot;;  // Please set the file ID.
const userValues = [&quot;john47@example.net&quot;, &quot;user2&quot;@abc.com&quot;, &quot;john47@example.net&quot;];  // Please set the email addresses.
const resources = userValues.map(e =&gt; ({role: &quot;writer&quot;, type: &quot;user&quot;, emailAddress: e}));
const boundary = &quot;xxxxxxxxxx&quot;;
const payload = resources.reduce((s, e, i) =&gt; {
s += &quot;Content-Type: application/http\r\n&quot; +
&quot;Content-ID: &quot; + i + &quot;\r\n\r\n&quot; +
&quot;POST https://www.googleapis.com/drive/v3/files/&quot; + fileId + &quot;/permissions?sendNotificationEmails=false&quot; + &quot;\r\n&quot; +
&quot;Content-Type: application/json; charset=utf-8\r\n\r\n&quot; +
JSON.stringify(e) + &quot;\r\n&quot; +
&quot;--&quot; + boundary + &quot;\r\n&quot;;
return s;
}, &quot;--&quot; + boundary + &quot;\r\n&quot;);
const params = {
method: &quot;post&quot;,
contentType: &quot;multipart/mixed; boundary=&quot; + boundary,
payload: payload,
headers: {Authorization: &quot;Bearer &quot; + ScriptApp.getOAuthToken()},
const res = UrlFetchApp.fetch(&quot;https://www.googleapis.com/batch&quot;, params);
console.log(res.getContentText())
Pattern 2:
In this pattern, a Google Apps Script library for the batch request is used.
Sample script:
Before you run the script, please set the file ID and email addresses, and please install the GAS library.
function myFunction() {
const fileId = &quot;###&quot;;  // Please set the file ID.
const userValues = [&quot;john47@example.net&quot;, &quot;user2&quot;@abc.com&quot;, &quot;john47@example.net&quot;];  // Please set the email addresses.
const reqs = userValues.map(e =&gt; ({
method: &quot;POST&quot;,
endpoint: &quot;https://www.googleapis.com/drive/v3/files/&quot; + fileId + &quot;/permissions?sendNotificationEmails=false&quot;,
requestBody: {role: &quot;writer&quot;, type: &quot;user&quot;, emailAddress: e},
}));
const requests = {batchPath: &quot;batch/drive/v3&quot;, requests: reqs};
const res = BatchRequest.Do(requests);
console.log(res.getContentText())
Please enable V8 at the script editor.
In above script, as a sample script, the maximum number of requests is 100. If you want to request over 100, please modify above script. Please be careful this.
References:
Batch request
Permissions: create
BatchRequest of GAS library
Comment for this answer: @JMR Thank you for replying. If my answer was useful for your situation, I'm glad.
Comment for this answer: Thank you Tanaike! I will definitely try this as I start diving into Drive API.

Here is another answer: you can design a function with an array like this:
function setpermisosreader(id,array){
var file = DriveApp.getFileById(id)
var editors = file.getEditors();
if (editors.length &gt; 0) {
for (var i = 0; i < array.length; i++) {
Drive.Permissions.insert({'role': 'reader','type': 'user','value': array[i]},id,
{'sendNotificationEmails': 'false' });};};
function setpermisoswriter(id,array){
var file = DriveApp.getFileById(id)
var editors = file.getEditors();
if (editors.length &gt; 0) {
for (var i = 0; i < array.length; i++) {
Drive.Permissions.insert({'role': 'writer','type': 'user','value': array[i]},id,
{'sendNotificationEmails': 'false' });};};
function test(){
var idfolder= &quot;idfile/folder&quot;
var array1 = [&quot;mail1&quot;,&quot;mail2&quot;,&quot;mail3&quot;]
var array2 = [&quot;mail1&quot;,&quot;mail2&quot;,&quot;mail3&quot;]
setpermisosreader(idfolder,array1)
setpermisoswriter(idfolder,array2)

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:03/26/20', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<google-apps-script><google-drive-api>', 'Title': "Drive.Permissions.insert (Value) - Drive API, can you use an array under 'value'?", 'ViewCount': 1873, 'id': 60859530, 'user_AboutMe': '', 'user_AccountId': 10786411.0, 'user_DisplayName': 'user7935276', 'user_Reputation': 167.0, 'user_Views': 102.0, 'user_id': 7935276.0}}