How do I script Google Drive to unshare all subfol

2019-02-14 08:41发布

问题:

Google Drive has a problem: if you unshare a top level folder everything in the folder is still shared...I'm not sure how such a large bug could survive but it does...details here:

http://productforums.google.com/forum/#!topic/drive/p3LZQnRNB24

I don't want to manually go through and unshare everything manually, so I'm thinking I could create a script that I could just plug in the folder ID into and have it do all the work.

How do I do this?

回答1:

this unshares someone from every folder and file in your drive.

// create a Google Document
// Tools / Script Editor
// paste this in
// update line 13 and save
// hit play to run. you may have to run several times as the script will die after 5 or 6 minutes.

function findSharedFolders() {
  // https://developers.google.com/apps-script/reference/drive/drive-app
  // https://developers.google.com/drive/web/search-parameters
  // http://stackoverflow.com/questions/21227771/how-do-i-script-google-drive-to-unshare-all-subfolders-and-files-given-a-folder
  // https://productforums.google.com/forum/#!topic/drive/p3LZQnRNB24

  var email = "unwanted-address@gmail.com"; // <<<<< INSERT THE UNDESIRED EMAIL ADDRESS HERE

  var body = DocumentApp.getActiveDocument().getBody();
  body.appendParagraph(email).setHeading(DocumentApp.ParagraphHeading.HEADING1);
  var folders = DriveApp.searchFolders("'"+email+"' in writers");  while (folders.hasNext()) unshare_(folders.next(), body, email);
  var files   = DriveApp.searchFiles(  "'"+email+"' in writers");  while (  files.hasNext()) unshare_(  files.next(), body, email);
}

function unshare_(file, body, email) {
// Logger.log(file.getName());
   var para = body.appendParagraph(file.getName());
   try { file.removeEditor(email); }
   catch (e) { para.setLinkUrl(file.getUrl()); para.appendText(" (" + e + ")"); }
}


回答2:

The code example posted above simply copy pasting and running after adding an email address spit out an error.

The error

http://i.imgur.com/fbJr6eG.png

The code suggested above for clarity.

// create a Google Document // Tools / Script Editor // paste this in // update line 13 and save // hit play to run. you may have to run several times as the script will die after 5 or 6 minutes.

function findSharedFolders() { // https://developers.google.com/apps-script/reference/drive/drive-app
// https://developers.google.com/drive/web/search-parameters // How do I script Google Drive to unshare all subfolders and files given a folder ID? // https://productforums.google.com/forum/#!topic/drive/p3LZQnRNB24

var email = "unwanted-address@gmail.com"; // <<<<< INSERT THE UNDESIRED EMAIL ADDRESS HERE

var body = DocumentApp.getActiveDocument().getBody();
body.appendParagraph(email).setHeading(DocumentApp.ParagraphHeading.HEADING1); var folders = DriveApp.searchFolders("'"+email+"' in writers"); while (folders.hasNext()) unshare_(folders.next(), body, email); var files = DriveApp.searchFiles( "'"+email+"' in writers"); while ( files.hasNext()) unshare_( files.next(), body, email); }

function unshare_(file, body, email) { // Logger.log(file.getName()); var para = body.appendParagraph(file.getName()); try { file.removeEditor(email); } catch (e) { para.setLinkUrl(file.getUrl()); para.appendText(" (" + e + ")"); } }



回答3:

The above answer works well but it is supposed to be run in a google doc (which it uses for logging out the information).

If you want to use it right from the script editor, just use the above code without the log stuff:

// create a Google Document
// Tools / Script Editor
// paste this in
// update line 13 and save
// hit play to run. you may have to run several times as the script will die after 5 or 6 minutes.

function findSharedFolders() {
  // https://developers.google.com/apps-script/reference/drive/drive-app
  // https://developers.google.com/drive/web/search-parameters
  // http://stackoverflow.com/questions/21227771/how-do-i-script-google-drive-to-unshare-all-subfolders-and-files-given-a-folder
  // https://productforums.google.com/forum/#!topic/drive/p3LZQnRNB24

  var email = "unwanted-address@gmail.com"; // <<<<< INSERT THE UNDESIRED EMAIL ADDRESS HERE

  var folders = DriveApp.searchFolders("'"+email+"' in writers");  while (folders.hasNext()) unshare_(folders.next(), email);
  var files   = DriveApp.searchFiles(  "'"+email+"' in writers");  while (  files.hasNext()) unshare_(  files.next(), email);
}

function unshare_(file, email) {
// Logger.log(file.getName());
   try { file.removeEditor(email); }
   catch (e) {  }
}