Google apps script 如何转让谷歌电子表格的所有权并成为一名浏览者
是否可以将所有者权限移交给其他用户,并使用应用程序脚本将前所有者更改为查看器 将所有者权限传输给另一个用户并成为查看者 我试图制作一个脚本,将所有者移交给另一个用户,并将前所有者更改为查看器,或禁止前所有者编辑谷歌电子表格。问题是该脚本由前所有者运行,并且该脚本无法删除前所有者的编辑权限 我试过的 我尝试了setViewer()、sheet.protect()和ContentRestrictions.readOnly,但它们都不是可行的解决方案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(
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服务一起使用)可能可以用于实现您的目标
- 我的提议脚本的流程如下
- 在当前阶段中,您是文件的所有者
- 在现阶段,你不是所有者。将您的权限从
更新为writer
reader
- 因为,当所有者被转移时,可以在“与我共享”文件夹中看到该文件
- 如果不需要创建快捷方式,请删除此部分
- 当运行上述脚本时,电子表格的所有者将更改为
。你的许可就变成了读者。因此,在本例中,您无法看到电子表格的脚本。请小心这个email
- 在这种情况下,更改电子表格的所有者后,您无法编辑电子表格和脚本。因此,请注意这一点。
我试图制作一个脚本,将所有者移交给其他用户,并将前所有者更改为查看器,或禁止前所有者编辑谷歌电子表格。
,我无法理解“所有者”和“其他用户”。例如,“所有者”是你吗?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);
}