【问题标题】:two onEdit(e) functions running simulatenously on Google Sheets App Script在 Google Sheets App Script 上同时运行的两个 onEdit(e) 函数
【发布时间】:2021-05-31 11:20:15
【问题描述】:

我正在处理一个项目,我的脚本中似乎有 2 个 onEdit(e) 函数。

第一个 onEdit 函数检查 C 列是否有更新,并在 A 列更新时输入时间戳。

第二个编辑函数检查一张表中的条目并编辑另一张表中的单元格(反之亦然)。

令我困惑的是,我没有编写第一个函数,我在网上找到它并忘记它是一个 onEdit 函数。然后我写了第二个函数,经过测试,工作正常。它还可以与第一个编辑功能一起使用。

据我了解,您不能在 Google Apps 脚本中拥有多个同名的方法...这是不正确的吗?

【问题讨论】:

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


    【解决方案1】:

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

    据我所知,您可以有许多同名的函数。但只有最后一个会起作用。休息函数将被忽略。

    【讨论】:

      【解决方案2】:

      答案:

      只会运行onEdit() 方法之一。

      代码修复:

      假设你有这样的代码:

      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 
      }
      

      您可以通过重命名它们并添加新的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 
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多