简体   繁体   中英

Google Apps Script Convert Array of Users to Array of Email Addresses

I'm trying to write a script in Google Apps Script.

I need to check if a user with an email user@domain.com is a viewer or editor of a folder. If the users is neither a viewer or editor I want to add that user as a viewer.

I'm using getViewers to get an array of viewers.

https://developers.google.com/apps-script/reference/drive/folder#getViewers()

There is no way as far as I can tell to look up user by email address, only email address by user: https://developers.google.com/apps-script/reference/drive/user#getEmail()

So I need to convert the array of viewers (users) to array of email addresses. Then I can use indexOf to check if user@domain.com is on that list.

How do I do that? From what I understand I need to use call or apply to apply a class to an array and get another array.

By the way I realize that this works, but I'm wondering if there is a faster or cleaner way to do it without having to loop through the array:

       var vieweremails = new Array();
    var viewers = folder.getViewers().concat(folder.getEditors()).concat(folder.getOwner()); 

   for (var i = 0; i < viewers.length; i++) {
   vieweremails[i] = viewers[i].getEmail()
 }

If you want to check the new user email with all the viewers emails, there is no other way other than looping through the array. Instead you can use addViewer(emailAddress) , as it has no effect if the user was already on the list of editors.

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM