【发布时间】:2017-11-24 03:33:17
【问题描述】:
一些上下文
- 我通过一个谷歌电子表格管理我的网络应用程序的翻译,我邀请翻译人员参与其中
- 共有 30 张工作表,每张代表应用程序(大应用程序)的一部分。
- 每张纸上有 14 列,1 列/语言。
我想做的事
由于我已经遇到过两次译者错误编辑错误栏的问题,我想设置 protected columns 以将每个译者的版本限制在他的语言栏(1 个译者 = 1 个电子邮件地址被授予访问电子表格)。
我是怎么做到的
手动设置很麻烦(重复性任务),如果翻译人员更换,必须重新设置。所以我为它写了一个脚本。
我是如何存储权限的:
var ProtectionsDefinitions = [{
langHeader: "en",
emails: ["toto@gmail.com"]
},{
langHeader: "fr",
emails: ["toto@gmail.com"]
}
...]
伪代码:
For every sheet:
For every language:
Protect the column whose header match the langHeader
执行实际工作的函数的真实代码:
function setProtection(range, rangeDesc, emails) {
// range = class range
// rangeDesc = string (description for protected range)
// emails = [toto@yopmail.com, tata@yopmail.com]
var protection = range.protect(); // Creates an object that can protect the range from being edited except by users who have permission.
// Until the script actually changes the list of editors for the range
// the permissions mirror those of the spreadsheet itself, which effectively means that the range remains unprotected.
protection.removeEditors(protection.getEditors()); // this takes more than 1s !
protection.setDomainEdit(true); // all users in the domain that owns the spreadsheet have permission to edit the protected range
protection.setDescription(rangeDesc);
if(emails.length > 0){
// this takes more than 1s !!
range.getSheet().getParent().addEditors(emails); // give the users permission to edit the spreadsheet itself, required for protection.addEditors()
protection.addEditors(emails); // give the users permission to edit the protected range
}
}
为什么不满意
- 函数
setProtection每个范围需要2s来保护 - 我有 30 张 * 14 列 = 420 个范围要保护。
- 整个执行超过了谷歌应用脚本允许的最长时间(~6min)
感谢日志工具,我跟踪了需要很多时间的行,请参阅函数中的 cmets。
我想知道我是否可以做点什么来让它发挥作用。
电子表格示例
- 翻译数据已被清理
- 工作表结构被保留,脚本在脚本编辑器中可用。
- https://docs.google.com/spreadsheets/d/1RRGSWsCz-Wyk3kxdldb6X0meYRV5xNEZKNQtLgfVv48/edit?usp=sharing
【问题讨论】:
标签: javascript google-apps-script google-sheets