firekid2018
firekid2018

Reputation: 145

How to get file sharing permission info using google app script?

i want to get which file sharing permission has been set to file with download links. i could not find any parameter for that. here is the code i have which can generate the file download link only.

function listFolderContents() {
  var foldername = 'Foldername';
  var folderlisting = 'listing of folder ' + foldername;

  var folders = DriveApp.getFoldersByName(foldername)
  var folder = folders.next();
  var contents = folder.getFiles();

  var ss = SpreadsheetApp.create(folderlisting);
  var sheet = ss.getActiveSheet();

  var file;
  var name;
  var link;
  var row;
  while(contents.hasNext()) {
    file = contents.next();
    name = file.getName() + "/" + file.getUrl() + "/" + file.getName();
    link = file.getUrl() + "/" + file.getName();
    sheet.appendRow( [name] );     
  }  
};

Upvotes: 0

Views: 1413

Answers (1)

Tanaike
Tanaike

Reputation: 201378

  • You want to retrieve the information of permissions of the file using Google Apps Script.

If my understanding is correct, how about this answer? You can retrieve the sharing permission and the sharing access by the methods of getSharingAccess() and getSharingPermission(). When these are used for your script, how about the following modification?

Modified script:

In this modification, the while loop is modified.

while(contents.hasNext()) {
  file = contents.next();
  name = file.getName() + "/" + file.getUrl() + "/" + file.getName();
  link = file.getUrl() + "/" + file.getName();

  var sharingPermission = file.getSharingPermission(); // Added
  var sharingAccess = file.getSharingAccess(); // Added

  sheet.appendRow( [name] );
}

Note:

  • I couldn't confirm about the format of output values. So I added only the 2 methods for your script.
  • Also if you want to retrieve the editors and viewers, how about adding the following script in the while loop?

    var editors = file.getEditors().map(function(e) {return e.getEmail()});
    var viewers = file.getViewers().map(function(e) {return e.getEmail()});
    

References:

If I misunderstood your question and this was not the result you want, I apologize.

Upvotes: 4

Related Questions