4

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.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Doug Fir
  • 19,971
  • 47
  • 169
  • 299
  • Tried to reproduce your error. The code worked fine in one Spreadsheet and it gives same error for other Spreadsheet. Try deleting the trigger and set it. I tried removing the trigger and add it back as a new new trigger and it worked. – KRR Aug 21 '15 at 21:24

1 Answers1

6

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 ] );

}
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • Thanks. I ended up writing to the sheet for debugging purposes. I found the built in debugger difficult to use too – Doug Fir Aug 23 '15 at 16:13
  • Cool logs were useless but the message is showing up through those other mediums thanks +1 – Dominic Oct 08 '20 at 03:09