首页 > 解决方案 > 为什么范围保护需要这么长时间

问题描述

我需要根据单元格中的名称对工作表上的单元格进行范围保护。我可以从他们的姓名中获取一个人的电子邮件地址并保护该单元格,以便只有他们可以编辑它。

将人员添加到范围保护似乎需要很长时间。我已经使用记录器来找出需要这么长时间的原因,它似乎 protection.addEditor() 需要 27700 毫秒,而 protection.removeEditors() 需要 21904 毫秒。这是正常的吗?

工作簿与一组大约 30 人共享,大约有 60 张工作簿,工作簿总共包含大约 300,000 个单元格。

任何想法为什么这需要这么长时间?

我收到一个错误:异常:访问具有 id 的文档时服务电子表格超时。每次运行脚本时都不会发生这种情况,但是当它发生时,它总是发生在我保护单元格的地方。我运行了一个通宵过程来寻找新条目并保护这些单元格,以便只有指定的人可以编辑。它用作轮班预订系统。

我希望有可能为每张纸一次性保护所有需要保护的细胞,但我找不到任何关于如何做到这一点的信息。

  function protectActiveCell() {
  
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var bookCell = sheet.getCurrentCell();
  var name = bookCell.getValue();
  var person = getPersonFromName(name);
  var email = person.email;
  *emphasized text*var others = getOtherUsersEmails(email);
  
  removeCellProtection(bookCell);

  var protection = bookCell.protect();
  
  SpreadsheetApp.flush();
  
  protection.addEditor(email);

  protection.removeEditors(others);
  
  SpreadsheetApp.flush();
  
}
  
  function removeCellProtection(cell) {
  
  // Remove range protections from specific cell.
  
  var sheet = cell.getSheet();

  var protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
  
  for (var i = 0; i < protections.length; i++) {

    var protection = protections[i];

    if (protection.getRange().getA1Notation() == cell.getA1Notation()) {

      protection.remove();

      SpreadsheetApp.flush();
    }
  }
}

标签: google-apps-scriptgoogle-sheets

解决方案


推荐阅读