Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between getActiveCell and getCurrentCell in Google Apps Script?

I've read the definitions a few times over and I still don't understand the difference.

getActiveCell: Range

  • https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#getActiveCell()
  • Summary: Returns the active cell in this sheet.
  • Description: Returns the active cell in this sheet.

getCurrentCell: Range

  • https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#getCurrentCell()
  • Summary: Returns the current cell in the active sheet or null if there is no current cell.
  • Description: Returns the current cell in the active sheet or null if there is no current cell. The current cell is the cell that has focus in the Google Sheets UI, and is highlighted by a dark border. There is never more than one current cell. When a user selects one or more cell ranges, one of the cells in the selection is the current cell.

There's this note in getActiveCell's documentation: (copy-pasted including grammatical errors)

It's preferrable to use getCurrentCell(), which the returns the current highlighted cell.

Which doesn't make it any clearer

like image 337
Pavel 'Strajk' Dolecek Avatar asked May 09 '26 11:05

Pavel 'Strajk' Dolecek


1 Answers

Answer:

getCurrentCell() returns the highlighted cell as seen by the dark border in the Sheets UI, whereas getActiveCell() returns the top-left cell of the selected range.

Example:

Say you have a range highlighted in a Sheet: B2:E10, for example. In the case where the you clicked on B2 and dragged down to E10:

enter image description here

Running the following code in the script editor:

function getCells() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1")
  console.log(sheet.getActiveCell().getA1Notation())
  console.log(sheet.getCurrentCell().getA1Notation())
}

You will see in the logs that both getActiveCell() and getCurrentCell() return B2.

However in the opposite scenario, where you clicked on E10 and dragged up to B2:

enter image description here

After running the same code, you will see that getActiveCell() still returns B2, but now getCurrentCell() returns E10.

like image 185
I hope this is helpful to you Avatar answered May 11 '26 14:05

I hope this is helpful to you



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!