没有看到来自onEdit触发器的日志 [英] Not seeing logs from onEdit trigger

查看:100
本文介绍了没有看到来自onEdit触发器的日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一个小函数:

$ $ $ $ $ $ $ $ $ $函数weeklyCurrency {
var changed_cell = e.range .getA1Notation();
Logger.log(changed_cell);
}

然后将其设置为可安装的onEdit触发器:

 资源>当前项目触发器>每周性状> onEdit 




已保存


转到工作表并输入任何值到单元格中,然后返回到编辑器并单击查看>日志


找不到用户日志。请运行您的脚本并重试。


我希望看到例如 A15



每当我进行编辑时,我都看不到任何东西?



我也尝试使用一个简单的触发器函数,将函数的名称改为 onEdit(),但是同样的事情;没有结果。

解决方案

我不知道这个问题的根本原因,但这正是我所描述的 flaky行为。


  • 每个脚本调用清除日志。每次触发 onEdit 函数时,都会得到一个新的执行环境,其中包括空白日志。 (所以如果你做了两个编辑,第二个将清除第一个日志。)

  • 有时日志根本不显示。这可能是因为清除过去的日志需要比录制当前日志更长的时间,因此两者都会被清除。



在某种程度上,你的代码适合我,因为我没有修改就运行它,并且有一个日志显示 - 不是每次都显示。这使得很难相信。



还有其他一些情况会导致内置记录器不合适。



由于您使用的是电子表格,因此您可以使用其他方式追踪代码的执行情况: >

toast messages

  var ss = e.range.getSheet()。getParent(); 
ss.toast(Changed cell:+ changed_cell);


  • 用户界面警报 $ b

      SpreadsheetApp.getUi()。alert(Changed cell:+ changed_cell); 


  • 浏览器 messages $ b

      Browser.msgBox(Changed cell:+ changed_cell); 


  • 写入电子表格

      var logSheet = ss.getSheetByName(Log)|| ss.insertSheet( 日志); 
    logSheet.appendRow([new Date(),Changed cell:+ changed_cell]);

    在这些选项中,我最喜欢这个。拥有一组持久的日志(每次不会重置)以及拥有时间戳是很好的。它仍然需要遵循适用于触发器的限制 - 一个简单的例如, onEdit()只能将日志写入到绑定的电子表格中。



    进一步处理,并覆盖内置Logger类,以便将整个项目中的所有日志记录到电子表格中。你可以在你知道吗? (您可以从客户端JavaScript登录到电子表格!),该主题涵盖了更加全面的内容。




  • 以下是所有这些选项的演示: $ b

      //来自http:// stackoverflow.com/a/32150927/1677912 
    function onEdit(e){
    var changed_cell = e.range.getA1Notation();

    //内置记录器
    Logger.log(changed_cell);

    //吐司
    var ss = e.range.getSheet()。getParent();
    ss.toast(Changed cell:+ changed_cell);

    // Alert
    SpreadsheetApp.getUi()。alert(Changed cell:+ changed_cell);

    //写入电子表格
    Browser.msgBox(Changed cell:+ changed_cell);
    var logSheet = ss.getSheetByName(Log)|| ss.insertSheet( 日志);
    logSheet.appendRow([new Date(),Changed cell:+ changed_cell]);

    }


    I have written a small function:

    function weeklyCurrency(e) {
      var changed_cell = e.range.getA1Notation();
      Logger.log(changed_cell);
    }
    

    Then set it up as an installable onEdit trigger:

    Resources > current projects triggers > weeklyCurreency > onEdit
    

    Saved

    Go to sheet and type any value into a cell, then return to the editor and click View > Logs:

    No user logs found. Please run your script and try again.

    I expected to see e.g. "A15".

    Why am I not seeing anything whenever I make an edit?

    I also tried using a simple trigger function by changing the name of the function to onEdit() but same thing; no results.

    解决方案

    I don't know the root cause of this problem, but it's what I've described as the "flaky behavior" of Logger.

    • Each script invocation clears the logs. Each time an onEdit function is triggered, it gets a new execution environment, which includes blank logs. (So if you do two edits, the second will clear the first log.)
    • And sometimes logs don't show up at all. This might be because the clearing of the past log takes longer to process than the recording of the current one, so both get wiped.

    In a way, your code "works for me", because I ran it without modification and did have a log show up - just not every time. That makes it hard to trust.

    There are other situations that make the built-in Logger unsuitable.

    Since you're working in a spreadsheet, you have other ways to trace the execution of your code:

    • toast messages

      var ss = e.range.getSheet().getParent();
      ss.toast( "Changed cell: "+changed_cell );
      

    • UI alerts

      SpreadsheetApp.getUi().alert( "Changed cell: "+changed_cell );
      

    • Browser messages

      Browser.msgBox( "Changed cell: "+changed_cell );
      

    • Write to spreadsheet

      var logSheet = ss.getSheetByName("Log") || ss.insertSheet("Log");
      logSheet.appendRow([ new Date(), "Changed cell: "+changed_cell ] );
      

      Of these options, I like this best. It's nice to have a persistent set of logs (that don't get reset every time), and to have timestamps. It still needs to follow the Restrictions that apply to triggers - a simple onEdit() can only write logs to the spreadsheet it's bound to, for example.

      You can take this last approach further, and over-ride the built-in Logger class so all logs across your project go to a spreadsheet. You can read more about that in Did you know? (You can log to a spreadsheet from client JavaScript!), which has a more thorough coverage of this topic.

    Here's a demo of all those options:

    // from http://stackoverflow.com/a/32150927/1677912
    function onEdit(e) {
      var changed_cell = e.range.getA1Notation();
    
      // Built-in Logger
      Logger.log(changed_cell);
    
      // Toast
      var ss = e.range.getSheet().getParent();
      ss.toast( "Changed cell: "+changed_cell );
    
      // Alert
      SpreadsheetApp.getUi().alert( "Changed cell: "+changed_cell );
    
      // Write to spreadsheet
      Browser.msgBox( "Changed cell: "+changed_cell );
      var logSheet = ss.getSheetByName("Log") || ss.insertSheet("Log");
      logSheet.appendRow([ new Date(), "Changed cell: "+changed_cell ] );
    
    }
    

    这篇关于没有看到来自onEdit触发器的日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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