简体   繁体   中英

Google Apps Script/ Drive API (Javascript) Remove from Shared With Me

Does anyone have any suggestions for how to remove a file from shared with me? I need to do this without actually revoking access, as the people are added via a contact group, so only removing 1 person will not work.

Things to note:

I know using this can find the file:

var files = DriveApp.searchFiles('sharedWithMe');
//then I set file = what I'm looking for.  

However, this will not remove the file from shared with me:

DriveApp.removeFile(file);

I've also tried brute forcing by patching the metadata, except it was to no avail. In addition, it is wrong to assume that shared with me is a folder, as when listing parent folders the file returns nothing.

Only DriveApp cannot completely remove files. So it is necessary to use Drive API. "removeFile()" is used for changing parent folder information. It cannot remove files. I prepared 2 samples for removing files. If you are owner of the file, you can remove it.

Move file to trash box and empty trash :

var file = DriveApp.getFilesByName("filename").next();
file.setTrashed(true)
Drive.Files.emptyTrash();

Remove directly file :

Drive.Files.remove("fileID");

It was confirmed that above script can remove file even if other users are using.

For using above script, it is necessary to enable Drive API at Advanced Google Services and Drive API at https://console.developers.google.com/ Developers Console Project.

Files without parent folder :

If you had used "removeFile()", files without the parent folder may be existing in your drive. You can confirm them following script. This can retrieve file name and file ID of files without the parent folder.

var result = [];
var files = DriveApp.getFiles();
while (files.hasNext()) {
  file = files.next();
  if (!file.getParents().hasNext()) {
    result.push([file, file.getId()]);
  }
}
Logger.log(result);

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