Home > Software design >  Clear all writers' permissions in Google Apps Script
Clear all writers' permissions in Google Apps Script

Time:08-06

I am using scripts to draw the list of permitted writers to a document new_ID from a list in sheet_roles using

for (var j = 3; j<=last_row_role; j  ) {
  if (range_roles.getCell(j, 4).getValue() != ''){
  add_Writer_Silent(new_ID, sheet_roles.getRange(j,3).getValue())
  }
}

where

 function add_Writer_Silent(docId, userEmail) {
  var permissionResource = {
    role: 'writer',   //can also have owner / organizer / fileOrganizer / writer / commenter / reader
    type: 'user',
    value: userEmail
  };
  var optionalArgs = {
    sendNotificationEmails: false,
    supportsAllDrives: true
  };
  Drive.Permissions.insert(permissionResource, docId, optionalArgs);
}

I would like to write another function that would update the list of writers. To do so, I need to clear the permissions list first and I am unable to find a way to do it in Permissions.

Can someone help me identify a way to remove all writers' permissions from a file using scripts?

CodePudding user response:

You'll need to first list all the permissions in the file and then get the ones with the role "writer" using its ID, you can try this:

function remove_permissions(docId) {
  var optionalArgs = {
    supportsAllDrives: true
  };
  
  var permissions = Drive.Permissions.list(docId, optionalArgs); //Saving all permissions in one Object.
  for(i=0; i< permissions.items.length; i  ){ //Iterating through all items in the object.
    if(permissions.items[i].role == "writer"){
      Drive.Permissions.remove(docId, permissions.items[i].id);
    }
  }
}

Reference:

  • Related