Google Apps 脚本,删除 copy/download 选项

Google Apps Script, get rid of copy/download option

我想知道是否有办法使用 Google Apps 脚本共享 Google 文档或 Google Sheet,但以编程方式禁用 copy/download 文档中 viewers/commentors 的功能?当正常共享 Google 文档或 Sheet 时,您可以转到设置图标并取消选择这些选项:

但是我可以通过 Google Apps 脚本来实现吗?

作为后续行动,有没有办法也为编辑器禁用这些选项?我希望能够与某人共享文档,让他们成为编辑,但阻止他们与其他任何人共享。谢谢。

在这种情况下,使用Drive API怎么样?我认为您可以使用 Drive API 设置它们。在这个答案中,使用了Drive API v3的“Files: update”方法。

示例脚本:

在您使用此脚本之前,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 表示“查看者和评论者可以看到下载、打印和复制的选项”

    • 看官方文档的时候,viewersCanCopyContent说的是Warning: This item is deprecated. Deprecated - use copyRequiresWriterPermission instead.。但是当使用copyRequiresWriterPermission时,这个检查就无法控制了。所以,在这个答案中,我使用了 viewersCanCopyContent.
    • 对于未来的更新,{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时,两个检查都检查

参考文献: