'onSelectionChange Google App Script sample

I am trying to figure out the official code sample of Google:

/**
 * The event handler triggered when the selection changes in the spreadsheet.
 * @param {Event} e The onSelectionChange event.
 */
function onSelectionChange(e) {
  // Set background to red if a single empty cell is selected.
  var range = e.range;
  if(range.getNumRows() === 1 
      && range.getNumColumns() === 1 
      && range.getCell(1, 1).getValue() === "") {
    range.setBackground("red");
  }
}

but I got the error message:

TypeError: Cannot read property 'range' of undefined...

Can somebody give me a hint? It is my first attempt with Google Apps Script.



Solution 1:[1]

If you use this code:

function onSelectionChange(e) {
  console.log(JSON.stringify(e));
}

This is what the event object looks like:

{"source":{},"range":{"columnEnd":31,"columnStart":31,"rowEnd":12,"rowStart":12},"user":{"email":"","nickname":""},"authMode":"LIMITED"}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cooper