Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 如何转让谷歌电子表格的所有权并成为一名浏览者_Google Apps Script_Google Sheets_Google Drive Api_Google Docs - Fatal编程技术网

Google apps script 如何转让谷歌电子表格的所有权并成为一名浏览者

Google apps script 如何转让谷歌电子表格的所有权并成为一名浏览者,google-apps-script,google-sheets,google-drive-api,google-docs,Google Apps Script,Google Sheets,Google Drive Api,Google Docs,是否可以将所有者权限移交给其他用户,并使用应用程序脚本将前所有者更改为查看器 将所有者权限传输给另一个用户并成为查看者 我试图制作一个脚本,将所有者移交给另一个用户,并将前所有者更改为查看器,或禁止前所有者编辑谷歌电子表格。问题是该脚本由前所有者运行,并且该脚本无法删除前所有者的编辑权限 我试过的 我尝试了setViewer()、sheet.protect()和ContentRestrictions.readOnly,但它们都不是可行的解决方案 removeditors()和setViewer(

是否可以将所有者权限移交给其他用户,并使用应用程序脚本将前所有者更改为查看器

将所有者权限传输给另一个用户并成为查看者 我试图制作一个脚本,将所有者移交给另一个用户,并将前所有者更改为查看器,或禁止前所有者编辑谷歌电子表格。问题是该脚本由前所有者运行,并且该脚本无法删除前所有者的编辑权限

我试过的 我尝试了setViewer()、sheet.protect()和ContentRestrictions.readOnly,但它们都不是可行的解决方案

  • removeditors()
    setViewer()
  • setViewer()
    方法对编辑器无效,并且在将removeEditors()应用于前所有者(当然,在更改所有者之后)之后,脚本无法执行
    setViewer()
    ,因为它不再具有权限

  • sheet.protect()
  • 该方法授予所有者和运行脚本的用户编辑受保护范围的权限。没有资格

  • contentRestrictions.readOnly
  • 编辑器可以解除限制。不可行

    • 内容限制代码:

      函数setReadOnly(){ var ss=SpreadsheetApp.getActive(); 风险值主管xxxxxxxx@gmail.com'; file=DriveApp.getFileById(ss.getId()); 试一试{ Drive.Files.update({ “writersCanShare”:false, “copyRequiresWriterPermission”:true, “contentRestrictions”:[{“只读”:true}] },ss.getId()); file.setSharing(DriveApp.Access.anywhere_与_链接,DriveApp.Permission.NONE); 文件所有者(主管); }捕获(e){ 控制台日志(e); } }

    我最近发现,
    contentRestrictions.readOnly
    已经在Google Drive API中实现。虽然我认为这是限制编辑修改文件内容的一个很好的方法,但我意识到作为编辑的用户可以通过单击“解锁”文件,即使文件被所有者锁定


    如果编辑器可以解决此限制,我不理解此属性的用法。中的解释说,我们可以使用此字段来防止对标题的修改、上载新版本和添加注释。但是,编辑器很容易解锁文件,而且查看器无论如何也无法修改文件。

    我在下面测试了自己的文件,只有我有权访问该文件

    代码: 测试中注意的事项:
    • 在将另一个用户设置为所有者之后,在自己身上运行
      removeditor
      ,将限制您甚至按照测试查看该文件
    • 所有者对removeEditor免疫(不能通过该方法删除)
    • 只有所有者可以将所有权设置为其他用户

    请看一个类似的答案,它会给你一些关于的见解。

    我相信你的现状和目标如下

    • 你有一个谷歌电子表格和一个脚本。你是它们的主人。
      • 从脚本中,该脚本是电子表格的容器绑定脚本
    • 运行脚本时,您希望将电子表格的所有者转移给其他用户
    • 在所有者被转移后,您希望保留电子表格作为读卡器而不是编写器的权限
    修改点:
    • 在本例中,我认为Drive-API的“Permissions:insert”和“Permissions:update”方法(在本例中,Drive-API与高级Google服务一起使用)可能可以用于实现您的目标
    • 我的提议脚本的流程如下
  • 为您检索权限ID。
    • 在当前阶段中,您是文件的所有者
  • 转移文件的所有者(在您的情况下,它是电子表格)。
    • 在现阶段,你不是所有者。将您的权限从
      writer
      更新为
      reader
  • 创建所有者转移到根文件夹的电子表格的快捷方式。
    • 因为,当所有者被转移时,可以在“与我共享”文件夹中看到该文件
    • 如果不需要创建快捷方式,请删除此部分
  • 此流反映到脚本中,如下所示

    修改脚本: 请将以下脚本复制并粘贴到Google电子表格容器绑定脚本的脚本编辑器中。并且,在使用此脚本之前

    注:
    • 当运行上述脚本时,电子表格的所有者将更改为
      email
      。你的许可就变成了读者。因此,在本例中,您无法看到电子表格的脚本。请小心这个
    • 在这种情况下,更改电子表格的所有者后,您无法编辑电子表格和脚本。因此,请注意这一点。
    参考资料:

    谢谢,但我需要维护前所有者的查看器权限,正如我在“RemoveEditor and setViewer”中提到的。要做到这一点,使用服务帐户的Ruben的回答是我从您提供的链接中得到的一种解决方案。然而,我仍然在寻找一种方法来完成这项简单的工作,而不需要一种超级管理员的权威。我能问一下你的情况的细节吗?1.关于
    我试图制作一个脚本,将所有者移交给其他用户,并将前所有者更改为查看器,或禁止前所有者编辑谷歌电子表格。
    ,我无法理解“所有者”和“其他用户”。例如,“所有者”是你吗?2.在您的情况下,当您想要使用脚本时,脚本运行的是谁?谁是脚本的所有者?3.您希望将电子表格的所有者转移给其他用户,并且希望保留作为读卡器而非编写器的权限。是我的
    function myFunction() {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var ssId = ss.getId();
      var file = DriveApp.getFileById(ssId);
      var owner = file.getOwner();
    
      var new_owner = 'new_owner@gmail.com';
      file.setOwner(new_owner);
      file.removeEditor(owner);    // After this line, you can't view/edit the file
      Logger.log(file.getOwner()); // Will fail
    }
    
    function myFunction() {
      const email = "###"; // Please set the email. The owner of the file is transferred to this email.
    
      const fileId = SpreadsheetApp.getActive().getId();
      
      // 1. Retrieve the permission ID for you. In the current stage, you are the owner of the file.
      const permissionId = Drive.Permissions.list(fileId).items[0].id;
    
      // 2. Transfer the owner of file (in your case, it's Spreadsheet.). In the current stage, you are not the owner.
      Drive.Permissions.insert({role: "owner", type: "user", value: email}, fileId, {sendNotificationEmail: false, moveToNewOwnersRoot: true});
    
      // 3. Update your permission from `writer` to `reader`.
      Drive.Permissions.update({role: "reader"}, fileId, permissionId);
    
      // 4. Create a shortcut of the owner-transferred Spreadsheet to the root folder. Because, when the owner is transferred, the file can be seen at the folder of "Shared with me".
      Drive.Files.insert({mimeType: MimeType.SHORTCUT, shortcutDetails: {targetId: fileId}}, null);
    }