【问题标题】:Protect a range with apps script takes too much time使用应用程序脚本保护范围需要太多时间
【发布时间】: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。

我想知道我是否可以做点什么来让它发挥作用

电子表格示例

【问题讨论】:

    标签: javascript google-apps-script google-sheets


    【解决方案1】:

    您可以申请Early Access Program,或者不使用单个脚本调用处理所有工作表/列,拆分作业。

    解决方案的想法:

    相关问答

    【讨论】:

    • 感谢您提及早期访问计划(不知道),也感谢您提及触发解决方案。 This answer 似乎提供了一种实现触发器解决方案的最小方法。在我的用例中,我认为我将把工作分成 3 块,实现触发器对我来说看起来像是过度优化,但了解这将如何解决我的问题非常有趣。
    猜你喜欢
    • 2022-12-03
    • 1970-01-01
    • 2018-04-16
    • 1970-01-01
    • 2016-08-20
    • 2012-07-20
    • 1970-01-01
    • 2011-04-13
    • 1970-01-01
    相关资源
    最近更新 更多