9
votes

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?

2
You can use the getActiveCell() method: Apps Script documentation - getActiveCellAlan Wells

2 Answers

2
votes

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
0
votes

If you just want to write into your current cell (from which a function is being called), you just create a script that returns a value/array. The value will be written in that location. Arrays will overflow into the rows below it.

It took me way to long to figure this out/remember this. I don't use sheets often.

function FillLoop(start, end){
    var retval = [];
        for (var x = 0; x< end-start; x++){
            retval[x] = x + start;
        }
    return retval;
}