0

I want to use the onSelectionChange trigger twice. When I select a cell the cell's background changes to blue. But when I select another cell, I would like for the previous cell's background to change from blue to red, but the current cell to change to blue.

I can get the current cell change to blue, but am unable to get them to red once I am no longer selecting them

I have 2 separate functions and I tried to give them different variable input arguments, but I'm not sure how to get them to work together.

function onSelectionChange(e) {
  rangeE = e.range;
  if (rangeE.getColumn() <= 5) {
    if (rangeE.getNumRows() === 1 &&
      rangeE.getNumColumns() === 1 &&
      rangeE.getCell(1, 1).getValue() !== '') {
      oldCell = rangeE.getCell(1, 1)
      rangeE.setBackground('blue');
    } 
  }
}

function onSelectionChange(q) {
  const rangeQ = q.range;
  currentCell = rangeQ.getCell(1, 1)
  if (currentCell !== oldCell){
  if (rangeQ.getColumn() <= 5) {
    if (rangeQ.getNumRows() === 1 &&
      rangeQ.getNumColumns() === 1 &&
      rangeQ.getCell(1, 1).getValue() !== '') {
      rangeE.setBackground('red');
    }
  }
  }
}
JRod
  • 5
  • 4
  • 2
    How about changing the function name of `onSelectionChange(e)` to `onSelectionChange1(e)` and `onSelectionChange2(e)`, and creating a new function like `function onSelectionChange(e) { onSelectionChange1(e); onSelectionChange2(e); }`? I think that @Rubén 's answer will be useful. https://stackoverflow.com/q/62602747 – Tanaike Feb 23 '23 at 23:08

0 Answers0