google-apps-script - Google Apps Script,去掉复制/下载选项

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

我想知道是否有办法使用 Google Apps 脚本共享 Google 文档或 Google 表格,但以编程方式禁用文档上查看者/评论者的复制/下载功能?正常共享 Google 文档或表格时,您可以转到设置图标并取消选择这些选项:
enter image description here
但是我可以通过 Google Apps Script 做到这一点吗?
作为对此的后续行动,是否有任何方法也可以为编辑器禁用这些选项?我希望能够与某人共享文档,让他们成为编辑器,但阻止他们与其他人共享。谢谢你。

最佳答案

在这种情况下,如何使用 Drive API?我认为您可以使用 Drive API 设置它们。在这个答案中,使用了 Drive API v3 的“文件:更新”的方法。
示例脚本:
在您使用此脚本之前,please enable Drive API at Advanced Google services .

function myFunction() {
  const fileId = "###";  // Please set the file ID.

  const url = "https://www.googleapis.com/drive/v3/files/" + fileId;
  const res = UrlFetchApp.fetch(url, {
    method: "patch",
    headers: {authorization: "Bearer " + ScriptApp.getOAuthToken()},
    contentType: "application/json",
    payload: JSON.stringify({viewersCanCopyContent: false, writersCanShare: false}),
  });
  console.log(res.getContentText())
  
  // DriveApp.createFile(blob)  // This comment line is used for automatically detecting the scope of "https://www.googleapis.com/auth/drive"
}
笔记:
  • writersCanShare用于“编辑者可以更改权限和共享”
  • viewersCanCopyContent用于“查看者和评论者可以看到下载、打印和复制的选项”
  • 看到官方文档时,viewersCanCopyContentWarning: This item is deprecated. Deprecated - use copyRequiresWriterPermission instead. .但是当 copyRequiresWriterPermission使用,此检查无法控制。所以,在这个答案中,我使用了 viewersCanCopyContent .
  • 对于 future 的更新,{viewersCanCopyContent: false, copyRequiresWriterPermission: false, writersCanShare: false}可能适合代替 {viewersCanCopyContent: false, writersCanShare: false} .

  • 上面的示例中,两个检查都未选中。
  • copyRequiresWriterPermission可以使用,我认为Drive API v2可能可以使用它如下。但是我现在测试的时候,好像无法控制“查看者和评论者可以看到下载、打印和复制的选项”。
      Drive.Files.patch({copyRequiresWriterPermission:false,writersCanShare:false}, fileId);
    

  • 例如,当 viewersCanCopyContent: true, writersCanShare: true被使用,两个检查都被检查。

  • 引用:
  • Files: update
  • Advanced Google services
  • 关于google-apps-script - Google Apps Script,去掉复制/下载选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63664349/

    相关文章:

    google-apps-script - 如何使用脚本在谷歌表格中创建定义的命名范围

    java - Google Drive API 多个单独的 get 请求检索所有父级很慢,有更好的方法吗?

    objective-c - 如何在没有 url 方案或外部服务器的情况下在 2 个本地 ios 应用程序之间共享文件?

    php - PHP上载约一千张图片?

    google-apps-script - 如何将链接附加到 Apps 脚本上的形状或文本框?

    google-apps-script - 如何以编程方式获取谷歌驱动器文件的文件(视频)持续时间?

    javascript - *服务器端* Google Apps 容器绑定(bind)脚本中的 jQuery 库

    node.js - Gaxios上的此文档不支持此操作-NodeJS-Spreadsheet Google

    google-api - 我可以将我的谷歌帐户配置为始终接受来 self 的应用程序的请求吗?

    iphone - UIFileSharingEnabled 使用文件夹