Javascript 使用应用程序保护范围脚本花费太多时间

Javascript 使用应用程序保护范围脚本花费太多时间,javascript,google-apps-script,google-sheets,Javascript,Google Apps Script,Google Sheets,某些上下文 我通过邀请翻译的谷歌电子表格管理我的网络应用的翻译 共有30张,每张代表应用程序的一部分(大应用程序) 每张图纸上有14列,1列/语言 我想做什么 由于我已经有两次遇到翻译人员错误编辑错误列的问题,我想将每个翻译人员的编辑限制为仅限于其语言的列(1个翻译人员=1个允许访问电子表格的电子邮件地址) 我是怎么做到的 手动设置是一项痛苦的任务(重复性任务),如果翻译人员发生变化,必须重新设置。所以我写了一个剧本 我如何存储权限: var ProtectionsDefinitions =

某些上下文

  • 我通过邀请翻译的谷歌电子表格管理我的网络应用的翻译
  • 共有30张,每张代表应用程序的一部分(大应用程序)
  • 每张图纸上有14列,1列/语言
我想做什么

由于我已经有两次遇到翻译人员错误编辑错误列的问题,我想将每个翻译人员的编辑限制为仅限于其语言的列(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
    每保护一个量程需要2秒
  • 我有30张纸*14列=420个范围需要保护
  • 整个执行过程超过了google apps脚本允许的最大时间(约6分钟)
由于使用了日志记录工具,我花了很多时间跟踪了这些行,请参见函数中的注释

我想知道我是否能做点什么使它工作起来

电子表格示例

  • 翻译数据已被清除
  • 保留图纸的结构,脚本在脚本编辑器中可用
您可以应用于或拆分作业,而不是使用单个脚本调用来处理所有工作表/列

解决方案的想法:

  • 并行处理(两个问题等效答案)
  • 使用连续批次库(请参阅至)
相关问答


感谢您提及早期访问计划(不知道),也感谢您提及触发解决方案。似乎提供了实现触发器解决方案的最小方法。在我的用例中,我想我将把工作分成3个部分,实现触发器对我来说似乎是一种过度优化,但是理解这将如何解决我的问题是非常有趣的。