List all shared users of a particular presentation

2019-05-23 07:20发布

问题:

We need a comprehensive list of people who have gained "Shared" viewing privileges of a particular presentation/doc on Google drive. We have a screen shot but it may NOT be enough. How may we retrieve this information programmatically?

回答1:

The attributes attached to a File object include three user-related items:

  • Owner - a single User object, retrieved using File.getOwner().
  • Editors - an array of Users who have editing privileges on the File, retrieved using File.getEditors(). Includes the Owner.
  • Viewers - an array of Users with read-only viewing privileges on the File, retrieved using File.getViewers(). Includes the Owner.

These can be obtained programmatically only by the file owner.

The following function is a utility from Restore trashed google drive files to a target folder. The getFileInfo() function returns an object with attributes of the provided File or Folder, including the Editors and Viewers. It ignores the owner in the Editors and Viewers lists. Feel free to adapt this to your own situation. Note: it relies on additional utility functions you'll find in this gist.

/**
 * Return an object containing relevant information about the given file.
 * See https://developers.google.com/apps-script/class_file.
 * From: https://gist.github.com/mogsdad/4644369
 *
 * @param {Object}  file   File or Folder object to examine.
 *
 * @return {Object}        Interesting file attributes.
 * <pre>{
 *     id: unique id (ID) of the folder or file
 *     name: the name of the File
 *     size: the size of the File
 *     type: The type of the file
 *     created: date that the File was created
 *     description: the description of the File
 *     owner: user id of the owner of the File
 *     otherViewers: list of user ids of viewers of the File, w/o owner
 *     otherEditors: list of user ids of editors of the File, w/o owner
 * }</pre>
 */
function getFileInfo (file) {
  var fileInfo = {
    id: file.getId(),
    name: file.getName(), 
    size: file.getSize(),
    type: (file.getMimeType) ? docTypeToText_(file.getMimeType()) : "folder",  // DriveApp Folders don't have getMimeType() method.
    created: file.getDateCreated(),
    description: file.getDescription(),
    owner: userNames([file.getOwner()],false),
    otherViewers: userNames(file.getViewers(),true),
    otherEditors: userNames(file.getEditors(),true)
    };
  return fileInfo;
}

Here's an example of a file's attributes:

{ "id": "0B2kSPNhhUowaRGZKY3VGLUZCREk",
  "name": "Rescued Files",
  "size": 0,
  "type": "folder",
  "created": "2016-06-13T20:18:14.526Z",
  "description": "",
  "owner": "user@example.com",
  "otherViewers": "none",
  "otherEditors": "none"
}

Bonus content

Below is a script that scans all files and folders in your Google Drive, generating logs with the user information. (It has no provision for avoiding timeouts, caveat emptor!)

/**
 * Generate logs with user sharing info for all files & folders.
 */
function showSharing() {
  var files = DriveApp.getFiles();
  var folders = DriveApp.getFolders();

  while (files.hasNext() || folders.hasNext()) {
    var iterator = files.hasNext() ? files : folders;

    Logger.log(JSON.stringify(getFileInfo(iterator.next())));
  }
}