Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference current cell in Google Apps script spreadsheet

A very basic question for google apps script: How do I reference the current cell from which the app script is being called for this current or active sheet in this spreadsheet?

like image 538
Rom Avatar asked Sep 12 '16 19:09

Rom


People also ask

How do you reference a cell in Google App Script?

You do the proper thing - work on an array of the Range's values - as opposed to repeatedly call over the interface via getCell or similar just to check the value. To reference the cell whose value is values[r][c] , note that the Javascript array is 0-base and the Range is 1-base indexed. So A1 = [0][0].

How do I get a specific cell in an app script?

Get selected cell value in your Google Sheet Script Now we have to add getCurrentCellValue() function. This function will get the value of the currently selected cell and then show it in a message box. In order to make it a little more informative, let us show the cell's A1 notation and then its value.


1 Answers

initial state (before script execution)

initial-state

example testing google app script

/**
 * Get selected cell coordinates and value, then set the cell value to 100
*/
function cellTester() {
  var spreadsheet = SpreadsheetApp.getActive();
  var sheet = spreadsheet.getActiveSheet();
  var cellRange = sheet.getActiveCell();
  var selectedColumn = cellRange.getColumn();
  var selectedRow = cellRange.getRow();
  Logger.log(`selectedColumn: ${selectedColumn}`);
  Logger.log(`selectedRow: ${selectedRow}`);
  Logger.log(`selected cell vale: ${cellRange.getValue()}`);
  cellRange.setValue(100);
}

final state (after the execution of the above script)

final-state

the log

    Log di Stackdriver
20 apr 2020, 23:23:56   Informazioni    selectedColumn: 3
20 apr 2020, 23:23:56   Informazioni    selectedRow: 2
20 apr 2020, 23:23:56   Informazioni    selected cell vale: 69
like image 86
Franco Rondini Avatar answered Sep 20 '22 19:09

Franco Rondini