Home > Enterprise >  How to select a cell in one column in the same row that the other cell, that is being edited in the
How to select a cell in one column in the same row that the other cell, that is being edited in the

Time:05-29


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

CodePudding user response:

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:

CodePudding user response:

You can use the simple trigger onEdit(), and get the column that was edited. If it's first column, you activate the cell in the same row and 3th column.

function onEdit(e) {
  const range = e.range
  const row = range.getRow()
  const col = range.getColumn()

  if(col == 1) SpreadsheetApp.getActiveSheet().getRange(row,3).activate()
}

You can try the function on this sheet.
(Note: You have to use it with a Google account in order to run.)

  • Related