Created
August 9, 2021 19:58
-
-
Save leoherzog/4b2b8a530e86d70ad01214acfa5153cf to your computer and use it in GitHub Desktop.
Google Shared Drive Viewers Automator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @OnlyCurrentDoc | |
* 1. Create a new Google Sheet (https://sheet.new) and remove all but one column | |
* 2. Add all of the email addresses in Column A that you'd like to have as viewers on the Shared Drive | |
* 3. Click Tools → Script Editor and copy/paste all of this code into the editor | |
* 4. Edit the Shared Drive ID on line 10 (https://drive.google.com/drive/folders/{Shared Drive ID}) | |
* 5. Click "▷ Run" in the top toolbar, allow authorization, and you're done! | |
*/ | |
const sharedDriveId = '0AAt_DfuXH73UUA9dVp'; | |
function updateMembers() { | |
let sharedDrive = DriveApp.getFolderById(sharedDriveId); // get "folder" (Shared Drive) object via DriveApp | |
let existingMemebers = sharedDrive.getViewers().map(e => e.getEmail()); // get it's viewers | |
let desiredMembers = SpreadsheetApp.getActiveSheet().getDataRange().getValues().flat().filter(e => !!e).map(e => e.toLowerCase().trim()); // the spreadsheet cells (a flattened array of arrays), filtered out blanks | |
let toAdd = desiredMembers.filter(x => !existingMemebers.includes(x)); // diff the two arrays to get emails that we need to add | |
let toRemove = existingMemebers.filter(x => !desiredMembers.includes(x)); // and emails that need to be removed | |
if (toAdd.length) console.log('Adding: ' + toAdd.join(', ')); | |
if (toRemove.length) console.log('Removing: ' + toRemove.join(', ')); | |
toAdd.forEach(email => { try {sharedDrive.addViewer(email)} catch(e) {console.warn(email + ': ' + e.message)} }); // attempt to add each | |
toRemove.forEach(email => { try {sharedDrive.removeViewer(email)} catch(e) {console.warn(email + ': ' + e.message)} }); // and remove each | |
if (ScriptApp.getScriptTriggers().length == 0) ScriptApp.newTrigger('updateMembers').timeBased().everyMinutes(15).create(); // then schedule to run this function every 15 minutes if it isn't already scheduled | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment