多个OnEdit功能的最佳做法 [英] Best Practices for Multiple OnEdit Functions

查看:82
本文介绍了多个OnEdit功能的最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题


  • 我有6个OnEdit函数,它们分别按预期工作,但在一起时却没有不能按预期工作。

脚本属性


  • 它们具有不同的名称-函数onEdit(e){code},函数onEdit1(e1){code},函数onEdit2(e2 ){code},函数onEdit3(e3){code},函数onEdit4(e4){code},函数onEdit5(e5){code}

  • 它们都在同一个.gs标签中

  • 其中有些变量具有相同的变量。例如OnEdit的 var range = e.range; 和OnEdit5的 var range = e5.range;

  • They have different names - function onEdit(e) {code}, function onEdit1(e1) {code}, function onEdit2(e2) {code}, function onEdit3(e3) {code}, function onEdit4(e4) {code}, function onEdit5(e5) {code}
  • They are all in the same .gs tab
  • Some of them have the same variables. For example OnEdit has var range = e.range; and OnEdit5 has var range = e5.range;

我的理解


  • 我相信您可以在同一.gs标签中运行多个OnEdit函数。这样对吗?还是我需要以某种方式创建新的.gs标签?

  • 我相信我的onEdit函数应以不同的方式命名,因此可以正确调用它们。这是正确的吗?还是我应该摆脱各种不同的功能,然后将它们整合为一个庞大的功能? (我想这会导致执行速度变慢,并且更多情况下无法隔离出错误的代码。)

  • 我相信在每个函数中创建的变量都是特定于该函数的。这是真的?还是它们互相影响?

我为什么要问这个问题

这个问题的迭代似乎曾经被问过。但是人们通常会建议将两个功能集成到一个大功能中,而不是让某人准备集成10-20个不同的OnEdit功能。它们也没有明确指示最佳编码实践。

Iterations of this question seem to have been asked before. But people generally give advice on integrating two functions into one big one, rather than preparing someone to integrate 10-20 different OnEdit functions. Nor do they give a clear indication of best coding practices.

我花了数小时阅读了这个主题,并觉得像我这样的脚本新手会从知道这一点中受益匪浅。

I've spent hours reading through this subject and feel that people new to scripts, like me, would greatly benefit from knowing this.

在此先感谢您的贡献!

推荐答案

注意:



  • 只能有一个同名函数。如果有两个,则后者将覆盖前者。

  • 名为 onEdit 的函数会自动触发(您猜对了!)编辑

  • 没有其他名称的简单触发,例如 onEdit1 onEdit2 ...。
  • 简单触发器的执行时间限制为30秒

  • 因此,在单个 code.gs 中文件或什至在一个项目中,只能有一个名为 onEdit 的函数并成功触发。

  • 如果创建多个项目, onEdit 将在每个项目中异步触发。但是可以创建的项目数量是有限制的,并且将应用其他配额。

  • 或者,您可以使用已安装的触发器:没有30s的限制。您还可以为函数使用任何名称。

  • 优化功能的最佳方法是除非绝对必要,否则请勿触摸电子表格。例如,脚本中的 sorting 各种值比在多个范围内多次调用 .sort 更好。工作表和脚本之间的交互越少,越好。高度优化的脚本只需要调用电子表格两次:一次获取数据,另一次设置数据。

  • 优化工作表调用次数后,可以优化脚本本身:控制逻辑,以便每次编辑仅完成必要数量的操作。例如,如果编辑在A1中(A1,B1是复选框,如果单击将分别清除A2:A10,B2:B10),则应检查是否单击了A1,如果单击,则清除范围并退出而不检查再次为B1。脚本优化至少需要JavaScript对象的基础知识。但是,这不如减少呼叫次数有效,这是所有应用程序脚本中最慢的部分。

  • Notes:

    • There can only be one function with a same name. If there are two, the latter will overwrite the former. It's like the former never existed.
    • A function named onEdit is triggered automatically on (You guessed it!)edit
    • There's no simple trigger for other names like onEdit1 or onEdit2....
    • Simple triggers are limited to 30 seconds of execution
    • So, in a single code.gs file or even in a single project, there can only be one function named onEdit and trigger successfully.
    • If you create multiple projects, onEdit will trigger in each project asynchronously. But there are limits to number of projects that can be created and other quotas will apply.
    • Alternatively, you can use installed triggers: which doesn't have limit of 30s. You can also use any name for your function.
    • The best way to optimize functions is to never touch the spreadsheet unless it is absolutely necessary. For example, sorting various values inside the script is better than repeatedly calling .sort on the multiple ranges multiple times. The lesser the interaction between sheets and scripts, the better. A highly optimized script will only require two calls to spreadsheet: one to get the data and the other to set the data.
    • After optimizing the number of calls to sheet, you can optimize the script itself: Control the logic such that only the necessary amount of operations are done for each edit. For example, if the edit is in A1(A1,B1 are checkboxes, if clicked clears A2:A10,B2:B10 respectively), then you should check if A1 is clicked and If clicked, clear the range and exit and not to check for B1 again. Script optimization requires atleast a basic knowledge of JavaScript objects. Nevertheless, this isn't as effective as reducing the number of calls-which is the slowest part of any apps script.
      • Best practices

      这篇关于多个OnEdit功能的最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆