2017-02-21 57 views
0

我想從有權訪問文件和文件夾的人員列表中刪除某人。如何快速從所有文件中刪除查看器或編輯器

我可以做到這一點使用下面的代碼,但由於我有很多文件需要很長時間,我不得不多次重新啓動過程並手動更改變量(var count = 5600)幾次以獲得任務完成。

這不可能是正確的解決方案,所以我想知道:
a)是否可以通過一次調用從所有文件(和文件夾)中刪除用戶?
b)如何快速(er)從一堆文件/文件夾中刪除查看器?

我知道我可以使用continuationtoken來避免在重啓之間更改代碼,但我沒有嘗試過。

function removeUser() 
{ 
    var userName = 'John Doe'; 
    removeUserFromViewersAndEditors_(userName); 
}; 

function removeUserFromViewersAndEditors_(userName) 
{ // Remove a user from viewers and editors 
    // Currently maximum runtime will be exceeded. Using a continuationtoken and automatically starting batches it is possible 
    // to make it run a sequence of batches and perform its task. 
    // As this script will not be run often, a manual approach is preferred for now. 
    // Start with              count = 0 and phase = 0 
    // After the program finishes change            phase = 1 and start again 
    // The program will stop on execution time limit reached change  count = ??    based on the log and start again 

    var count = 5600; // Update if maximum runtime exceeded 
    var phase = 1; // Update if maximum runtime exceeded 

    var user    = findUser_(userName, phase, count); 
    var totalRemovedFolders = 0; 
    var totalRemovedFiles = 0; 
    if (user != null) 
    { 
    if (phase == 0) 
    { 
     var countFolders = 0; 
     var folders = DriveApp.getFolders(); 
     while(folders.hasNext()) 
     { 
      if (removeUserFromFolderOrFile_(folders.next(), user) == true) totalRemovedFolders++; 
      countFolders++; 
     } 
     Logger.log('testRemoveUser Folders : count= ' + countFolders + ' totalRemovedFolders= ' + totalRemovedFolders); 
    } 
    else if (phase == 1) 
    { 
     var countFiles = count; 
     var files = DriveApp.getFiles(); 
     while(count--) files.next(); // Skip a number of files 

     while(files.hasNext()) 
     { 
      if (removeUserFromFolderOrFile_(files.next(), user) == true) totalRemovedFiles++; 
      countFiles++; 
      if ((100 * ((countFiles/100) | 0)) == countFiles) 
      { 
       Logger.log('phase= ' + phase + ' count= ' + countFiles + ' user= ' + userName + ((user == null) ? '' : ' found totalRemovedFiles= ' + totalRemovedFiles)); 
      } 
     } 
     Logger.log('testRemoveUser Files : count= ' + countFiles + ' totalRemoved= ' + totalRemovedFiles); 
    } 
    } 

    Logger.log('totalRemoved= ' + (totalRemovedFolders + totalRemovedFiles)); 


//////////////////// Internal functions removeUserFromViewersAndEditors_ 
    function removeUserFromEvents_(userName) 
    { // Must be executed BEFORE the user will be removed from memberNames (so : delete user from contacts AFTERWARDS !!) 
     var removed = 0; 
     var dateFrom = new Date(); 
     var future = Date.now() + 2 * 365 * 24 * 60 * 60 * 1000 ; // 2 years from now (milliseconds) 
     var dateUntil = new Date(future) 
     var fileName = OPA_Agenda; 
     var cal  = CalendarApp.getCalendarsByName(OPA_Calendar)[0]; 
     var events = cal.getEvents(dateFrom, dateUntil);   // All events within the timeframe 
     var numEvents = events.length; 

     var memberNames = getMemberNames_(); 
     var memberEmails = getMemberEmails_(); 
     var numMembers = memberNames.length; 
     for (var i=0; i<numMembers; i++) 
     { 
     if (userName == memberNames[i]) 
     { // found 
      var email = memberEmails[i]; 

      for (var j=0; j<numEvents; j++) 
      { 
       var event  = events[j]; 
       var guests = event.getGuestList(); 
       var numGuests = guests.length; 
       for (var k=0; k<numGuests; k++) 
       { 
        if (email == guests[k].getEmail()) 
        { 
        event.removeGuest(email); // Removed from this event 
        Logger.log(userName + ' removed from event : ' + event.getTitle() + ' at ' + event.getStartTime()); 
        removed++; 
        break; 
        } 
       } 

      } 
      break; 
     } 
     } 

     Logger.log(userName + ' removed from ' + removed + ' events'); 
     return removed; 
    } 

    function removeUserFromFolderOrFile_(fileOrFolder, user) 
    { // Remove <user> as editor or viewer from a file or a folder 
     var removed = false; 
     var array = []; 
     var userName = user.getName(); 
     for (var i=0; i<2; i++) 
     { 
     if (i == 0)  array = fileOrFolder.getEditors(); 
     else if (i == 1) array = fileOrFolder.getViewers(); 
     var size = array.length; 
     while(size--) 
     { 
      if (userName == array[size].getName()) 
      { 
       if (i == 0)  fileOrFolder.removeEditor(array[size]); 
       else if (i == 1) fileOrFolder.removeViewer(array[size]); 
    Logger.log('User ' + userName + ' removed from ' + fileOrFolder.getName());   
       removed = true; 
       break; 
      } 
     } 
     } 

     return removed; 
    } 

    function findUser_(name, phase, count) 
    { 
     var iterator = null; 
     var user = null; 
     for (var i=0; i<2; i++) 
     { 
     Logger.log('findUser phase ' + i); 
     if (i == 0) 
     { 
      iterator = DriveApp.getFolders(); 
      if (phase == 0) 
      { // Skip <count> folders 
       for (var j=0; j<count; j++) var object = iterator.next(); 
      } 
      else iterator = null; 
     } 
     else if (i == 1) 
     { 
      iterator = DriveApp.getFiles(); 
      if (phase == 1) 
      { // Skip <count> files 
       for (var j=0; j<count; j++) var object = iterator.next(); 
      } 
      else 
      { // Just finished checking folders 
       count = 0; 
       phase = 1; 
      } 
     } 

     if (iterator != null) 
     { 
      while (iterator.hasNext() && (user == null)) 
      { 
       var object = iterator.next(); 
       var array = []; 
       for (var j=0; j<2; j++) 
       { 
        if (j == 0)  array = object.getEditors(); 
        else if (j == 1) array = object.getViewers(); 
        var size = array.length; 
        while(size--) 
        { 
        if ((name == array[size].getName()) || (name == array[size].getEmail())) 
        { // Found 
         user = array[size]; 
         break; 
        } 
        } 
       } 

       count++; 
       if ((100 * ((count/100) | 0)) == count) 
       { 
        Logger.log('phase= ' + phase + ' count= ' + count + ' user= ' + name + ((user == null) ? '' : ' found')); 
       } 
      } 
     } 
     } 

     Logger.log('findUser(' + name + ') ' + ((user == null) ? 'failed' : 'succeeded')); 
     return user; 
    } 
//////// End internal functions removeUserFromViewersAndEditors_ 
} 

回答

0

我沒有看到您的用例的任何示例。你可能會喜歡這個related product forum但使用GAM命​​令參考:

gam all users show filelist query "'[email protected]' in writers or '[email protected]' in readers" id | gam csv - gam user ~Owner delete drivefileacl ~id [email protected] 

的第一個命令將產生與供應商的電子郵件地址共享的所有文件的CSV列表爲域內的所有用戶(讀或寫訪問) 。如果您的域中有很多用戶,則需要一段時間才能生成此列表。 |之後的第二個命令管道讀取此CSV列表並對與供應商共享的每個文件執行刪除ACL操作。

您也可以檢查這個thread

相關問題