George
George

Reputation: 123

How to select a cell in one column in the same row that the other cell, that is being edited in the other column using Google Apps Script?


I've been wondering if there was a way to select a cell in a particular column, when the other particular column has been edited, using Google Apps Script?
For example: **If someone edits B3, my script should select C3 (if someone edits B23, then C23), but someone edits let's say A5- then nothing happens.**
See the example picture below
Thank you in advance!


enter image description here

Upvotes: 1

Views: 678

Answers (2)

Cooper
Cooper

Reputation: 64100

Try this

function onEdit(e) {
  const sh = e.range.getSheet();
  if (sh.getName() == `YOUR SHEET NAME` && e.range.columnStart == 2 ) {
    e.range.offset(0,1).activate();
  }
}

activate

Upvotes: 1

NEWAZA
NEWAZA

Reputation: 1630

It's possible through the onEdit trigger, data from the e/event object, and .activate().

Try:

function onEdit(e) {

  if (e.source.getActiveSheet().getName() === `Sheet1`) {

    if (e.range.rowStart === 5 && e.range.columnStart === 1) return

    e.source.getActiveSheet()
            .getRange(e.range.rowStart, e.range.columnStart+1)
            .activate()

  }

}

Commented:

function onEdit(e) {

  // If the sheet ('Sheet1') was edited.. 
  if (e.source.getActiveSheet().getName() === `Sheet1`) {
    // "Cancel" if A5 was selected.
    if (e.range.rowStart === 5 && e.range.columnStart === 1) return

    // Get the active sheet
    e.source.getActiveSheet()
    // Select the range of "edited row", "edited column +1"
            .getRange(e.range.rowStart, e.range.columnStart+1)
            // "Select" the cell.
            .activate()

  }

}

If you would like to 'ignore' more than A5, please let me know and I can update the examples for you.

Learn More:

Upvotes: 2

Related Questions