简体   繁体   English

在 Google Sheets App Script 上同时运行的两个 onEdit(e) 函数

[英]two onEdit(e) functions running simulatenously on Google Sheets App Script

I am working on a project and it seems I have got 2 onEdit(e) functions in my script.我正在做一个项目,似乎我的脚本中有 2 个 onEdit(e) 函数。

The first onEdit function checks column C for an update and enters a timestamp into column A when it is.第一个 onEdit function 检查列 C 是否有更新,并在 A 列中输入时间戳。

The second an edit function checks entries in 1 sheet and edits a cell in another sheet (and vice versa).第二个编辑 function 检查一张表中的条目并编辑另一张表中的单元格(反之亦然)。

What im confused about is, i didn't write the first function, i found it online and forgot it was an onEdit function.我感到困惑的是,我没有写第一个 function,我在网上找到它并忘记它是一个 onEdit function。 Then i wrote the second function, tested, works fine.然后我写了第二个 function,经过测试,工作正常。 And it also works alongside the 1st edit function.它还可以与第一次编辑 function 一起使用。

It was my understanding that you couldn't have multiple methods with the same name in google apps script... is this incorrect?据我了解,您不能在 google 应用程序脚本中使用多个具有相同名称的方法……这是不正确的吗?

Thanks!谢谢!

 function main() { console.log("Function 1"); } function main() { console.log("Function 2"); } main(); // Output: "Function 2"

As far as I can tell you can have many functions with the same name.据我所知,你可以有许多同名的函数。 But only last one will work.但只有最后一个会起作用。 Rest functions will be ignored. Rest 函数将被忽略。

Answer:回答:

Only one of the onEdit() methods will be run.只会运行其中一种onEdit()方法。

Code Fix:代码修复:

Assuming you have code like this:假设你有这样的代码:

function onEdit(e) {
  // code to check column C for an update and enter a timestamp into column A 
}

function onEdit(e) {
  // code to check entries in 1 sheet and edit a cell in another sheet 
}

You can allow both to run by renaming them and adding a new onEdit() :您可以通过重命名它们并添加新的onEdit()来允许两者运行:

function onEdit(e) {
  checkColC(e)
  checkEntries(e)
}

function checkColC(e) {
  // code to check column C for an update and enter a timestamp into column A 
}

function checkEntries(e) {
  // code to check entries in 1 sheet and edit a cell in another sheet 
}

I hope this is helpful to you!我希望这对你有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM