google-apps-script - 为什么范围保护需要这么长时间
问题描述
我需要根据单元格中的名称对工作表上的单元格进行范围保护。我可以从他们的姓名中获取一个人的电子邮件地址并保护该单元格,以便只有他们可以编辑它。
将人员添加到范围保护似乎需要很长时间。我已经使用记录器来找出需要这么长时间的原因,它似乎 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();
}
}
}
解决方案
推荐阅读
- c# - 具有第 3 次集成的 3 层架构
- reactjs - 将 docker-compose 环境变量的值转换为服务 ip 地址
- angular - 输入装饰器不显示来自父组件的数据
- c# - 带有凭据的 C# WebProxy
- python - 以有效的方式加入具有格式的元组列表
- css - Side horizontal lines with flexbox
- objective-c - 带有objective-c的Json反序列化在版权符号上失败
- influxdb - 迭代 InfluxDB 中的大量数据
- git - Github:使用 ssh 密钥仅在一个存储库或组织中使用
- javascript - 除了两小时前更新的文件外,如何查找文件?