Last active
November 4, 2015 00:44
-
-
Save brainysmurf/663a3f6f2425300ad72c to your computer and use it in GitHub Desktop.
Close Down and Open Up Editors via Script
This file contains hidden or 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
/* | |
Use this at your own risk. | |
1) Enter the ID of the folder which contains the files you would like to remove and restore edit access | |
2) Then running triggerCloseDown will save those editors and remove edit access (and view access) for all editors of that file | |
3) Then running triggerOpenUp will lookup editors of each file and restore editing access | |
NOTES: | |
- You have to run triggerCloseDown first in order to save the editors | |
- The editor information is stored on a per-file basis. | |
- After the first run, the file's permissions and so it no longer inherits the parent folder permissions | |
- You currently have to store all these files in a folder to work | |
*/ | |
var FOLDERID = ''; // The ID of the folder, can be taken from the URL | |
function getFolder_(folderID) { | |
return DriveApp.getFolderById(folderID); | |
} | |
function triggerCloseDown() { | |
closeDown_(FOLDERID); | |
} | |
/* | |
closeDown | |
*/ | |
function closeDown_(folderId) { | |
var folder = getFolder(folderId); | |
var files = folder.getFiles(); | |
var editorsIdHash = {}; | |
while (files.hasNext()) { | |
var file = files.next(), fileId; | |
file.getEditors().forEach(function (user, index, arr) { | |
fileId = file.getId(); | |
if (!editorsIdHash.hasOwnProperty(fileId)) { | |
editorsIdHash[fileId] = []; | |
} | |
editorsIdHash[fileId].push(user.getEmail()); | |
file.removeEditor(user.getEmail()); | |
}); | |
} | |
Logger.log(editorsIdHash); | |
var scriptProperties = PropertiesService.getScriptProperties(); | |
for (var fileId in editorsIdHash) { | |
scriptProperties.setProperty(fileId, editorsIdHash[key].join(',')); | |
} | |
} | |
function triggerOpenUp() { | |
openUp_(FOLDERID); | |
} | |
/* | |
openUp | |
*/ | |
function openUp_(folderId) { | |
var folder = getFolder_(folderId); | |
var files = folder.getFiles(); | |
var scriptProperties = PropertiesService.getScriptProperties(); | |
while (files.hasNext()) { | |
var file = files.next(); | |
var fileId = file.getId(); | |
var editors = scriptProperties.getProperty(folderId + fileId); | |
editors.split(',').forEach(function (email, index, arr) { | |
// Uncomment if you don't mind the editors getting notifactions all the time | |
// But I am sure they will mind. | |
//file.addEditor(email); | |
// This requires enabling the Drive API in the developer's console | |
// And in Resources -> Advanced Google Services. | |
// but users don't get the annoying email. | |
Drive.Permissions.insert( | |
{ | |
'role': 'writer', | |
'type': 'user', | |
'value': email | |
}, | |
fileId, | |
{ | |
'sendNotificationEmails': 'false' | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment