Retrieving Active Range in Google Sheets on Change Event

I’m working with a function that looks like this:

function editTrigger(e) {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var currentSheet = spreadsheet.getSheets()[0];
  // Fetches the currently selected range
  var selectedRange = currentSheet.getActiveRange();

  Logger.log(selectedRange.getA1Notation());
}

Currently, this function is activated during a ‘change’ event in my spreadsheet. Initially, it was set to ‘on edit’ (hence the name), which doesn’t respond to background color changes, a crucial aspect of my task.

Every time it runs, the log always outputs A1 regardless of the active selection, and the same issue occurs when I replace getActiveRange() with getActiveCell().

Is there a method to access the actual selected range when the script executes?

The behavior you’re experiencing with .getActiveRange() or .getActiveCell() returning always the same range is likely because the script is not triggered by a user action in the UI but by a change event. Change events typically don’t capture the current user-selected cell or range because they are not directly triggered by active selection changes. You might need to consider storing the selection elsewhere beforehand or using a global variable to maintain the state of selection prior to the change event. Alternatively, an ‘on edit’ trigger could be set up alongside to at least log or store selections that will indirectly reflect changes happening later.