Majal
Majal

Reputation: 1741

Is it possible to make a getCommenters() functionality in Google Apps Script?

The DriveApp > File class of Google Apps Script has these methods available:

But getCommenters method is not available. (This is interesting since addCommenter, addCommenters, and removeCommenter methods are available.)

I'm making a script where I'm changing file and folder permissions based on email addresses in a Spreadsheet. I would like to have a getCommenters functionality so I can compare if the commenter emails in the Spreadsheet are already commenters to the file, so there will be no need to again push those emails using addCommenters. (Pushing it will generate a new email to the user.) There is no problem in doing this with Viewers and Editors, as the methods are available.

Question: Is it possible to create a function that will mimic the supposed functionality of getCommenters? Or at least pull an array of email addresses of the commenters?

Upvotes: 0

Views: 534

Answers (2)

Majal
Majal

Reputation: 1741

Getting the right nudge from @TheMaster's answer, this code worked for what I needed:

let file = DriveApp.getFileById('xxxxxxxxxxx-FILE_ID-xxxxxxxxxxx');

let userlist = file.getViewers().filter((user) => file.getAccess(user) == 'COMMENT');

let emaillist = [];

for (var i in userlist) {
  emaillist.push(userlist[i].getEmail());
}

Thereafter, emaillist becomes an array of email addresses having COMMENT permissions.

Upvotes: -1

TheMaster
TheMaster

Reputation: 50452

getViewers() returns viewers and commentators. You can filter the list using getAccess():

const getCommenters = (file) => 
  file.getViewers().filter(user=>
    file.getAccess(user) == "COMMENT")

Upvotes: 2

Related Questions