Google Apps脚本,摆脱复制/下载选项 示例脚本:注意:参考文献:

问题描述

我想知道是否有一种方法可以使用Google Apps脚本共享Google Doc或Google Sheet,但是可以通过编程方式为文档中的查看者/注释者禁用复制/下载功能?正常共享Google文档或表格后,您可以转到设置图标并取消选择以下选项:

enter image description here

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

作为对此的后续措施,是否也有办法为编辑器禁用这些选项?我很希望能够与某人共享文档,让他们成为编辑者,但又阻止他们与其他人共享文档。谢谢。

解决方法

在这种情况下,如何使用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
    • 对于将来的更新,{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时,将同时选中两项检查。

参考文献:

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...