google-apps-script - 列出 Google 云端硬盘上特定演示文稿/文档的所有共享用户

标签 google-apps-script google-drive-api

我们需要一份完整的列表,其中包含已获得 Google 云端硬盘上特定演示文稿/文档的“共享”查看权限的人员。我们有屏幕截图,但可能还不够。我们如何以编程方式检索此信息?

最佳答案

附加到 File 对象的属性包括三个与用户相关的项目:

  • Owner - 单个 User 对象,使用 File.getOwner() 检索。
  • Editors - 对文件具有编辑权限的 Users 数组,使用 File.getEditors() 检索。包括所有者。
  • Viewers - 对文件具有只读查看权限的 Users 数组,使用 File.getViewers() 检索。包括所有者。

这些只能由文件所有者以编程方式获得。

以下函数是来自 Restore trashed google drive files to a target folder 的实用程序. getFileInfo() 函数返回一个对象,该对象具有提供的 FileFolder 的属性,包括编辑器和查看器。它会忽略编辑者和查看者列表中的所有者。请随意根据您自己的情况进行调整。注意:它依赖于您会发现的其他实用函数 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;
}

这是文件属性的示例:

{ "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"
}

奖励内容

下面是一个脚本,可以扫描您 Google Drive 中的所有文件和文件夹,生成包含用户信息的日志。 (它没有避免超时的规定,买者自负!)

/**
 * 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())));
  }
}

关于google-apps-script - 列出 Google 云端硬盘上特定演示文稿/文档的所有共享用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14397910/

相关文章:

php - 调用未定义的方法 Google_Service_Drive_FileList::getItems()

java - Google Drive API v2 q=sharedWithMe 从 "My Drive"文件夹返回文件

javascript - 从数组创建链接列表

javascript - Google Apps 脚本基于时间的触发器,根据电子表格中的值在自定义日期和时间触发

google-apps-script - getThreads 的超大标签

google-drive-api - 如何知道谷歌帐户是否在谷歌应用程序域下?

node.js - 不断收到 API 返回错误 : Error: Daily Limit for Unauthenticated Use Exceeded. 继续使用需要注册

google-apps-script - 如何获取当前选定的单元格/范围?

javascript - JS 按两列日期对数组进行排序

google-sheets - Google OAuth 2.0 的长期访问 token