Search code examples
google-apps-scripttriggers

Why won't apps-script "onEdit" simple trigger work?


I added this function, but editing any cell won't leave the desired note. Why?

function onEdit(e) {
  // Set a comment on the edited cell to indicate when it was changed.
  const range = e.range;
  range.setNote('Last modified: ' + new Date());
}

Solution

  • In the first one I added (") and it works according to your requirements, in the second one I made a variant.

    function onEdit(e) {
      // Get the edited cell
      const range = e.range;
    
      // Sets the comment to the current date
      range.setNote("Last modification: " + new Date());
    }

    function onEdit(e) {
      var range = e.range;
      var timestamp = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'yyyy-MM-dd HH:mm:ss');
      range.setComment('Last modified: ' + timestamp);
    }