【问题标题】:Copy spreadsheet with google script and set permission to each sheet使用谷歌脚本复制电子表格并为每张工作表设置权限
【发布时间】:2020-10-01 07:26:06
【问题描述】:

你能帮我吗?在谷歌脚本中真的不可能吗? 我们有: 电子表格 1

  • sheet1 - 编辑 AAA@gmail.com 的权限
  • sheet2 - 编辑 BBB@gmail.com 的权限 然后我们在 Spreadsheet1 中创建一个名为“Spreadsheet 2”的运行脚本副本:
file.makeCopy(name, destination);

我需要以相同的方式保存编辑电子表格 2 的权限:用于 AAA 的 sheet1 和用于 BBB 的 sheet2

找到这个:https://developers.google.com/apps-script/reference/drive/#permission,但它适用于驱动器级别。

Tnx 寻求帮助

附:这个东西可以提供帮助,但是在创建新电子表格的副本时如何实现?并授予运行脚本的权限https://developers.google.com/apps-script/reference/spreadsheet/range#protect()

【问题讨论】:

标签: google-apps-script spreadsheet


【解决方案1】:
  • 通过addEditors(emailAddresses) 与您想要的编辑共享复制的文件。
  • 通过getId() 获取复制的文件ID 并使用它来检索电子表格。
  • 使用Protection 保护每个工作表免受所有人的攻击,但运行脚本的用户和所需的编辑器除外。

代码示例:

var newFile = file.makeCopy(name, destination);
newFile.addEditors(["AAA@gmail.com", "BBB@gmail.com"]);
var spreadsheet = SpreadsheetApp.openById(newFile.getId());
var sheet1 = spreadsheet.getSheetByName("Sheet1");
var sheet2 = spreadsheet.getSheetByName("Sheet2");
protectSheet(sheet1, "AAA@gmail.com");
protectSheet(sheet2, "BBB@gmail.com");

protectSheet 在哪里:

function protectSheet(sheet, editor) {
  var protection = sheet.protect();
  var me = Session.getEffectiveUser();
  protection.addEditor(me);
  protection.removeEditors(protection.getEditors());
  if (protection.canDomainEdit()) {
    protection.setDomainEdit(false);
  }
  protection.addEditor(editor);
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-15
相关资源
最近更新 更多