The solution shared by the OP but taking advantage of the on edit event object
function onEdit(e) {
/** Settings */
var range = s.getRange("C3:W26");
var name = "Estado Paises";
var exceptionColumn = 2;
var s = e.range.getSheet();
/** Conditional Statement */
if(
e.range.rowStart >= range.getRow() && e.range.rowStart <= range.getLastRow() &&
e.range.columnStart >= range.getColumn() && e.range.columnStart <= range.getLastColumn()
) {
// do something
if( s.getName() == name ) { //checks that we're on the correct sheet
if( e.range.columnStart != exceptionColumn ) { //checks the column
var date = Utilities.formatDate(new Date(), "GMT-03:00", "dd/MM/yy");
s.getRange('X' + e.range.rowStart).setValue(date);
}
}
} else {
// do nothing
}
}
Technically an on edit triggered script will be executed whenever the spreadsheet be edited by using the Google Sheets user interface so we can only limit only some part of the script to be executed by using conditional expressions on JavaScript statements like if...else
, switch
, just to mention the most commonly used.
The on edit event object has several properties that might be helpful specially the range
that returns a Class Range object for the edited range but also include the properties rowStart
, rowEnd
, columnStart
and columnEnd
that return the row and column numbers of the top-left and bottom-right cells respectively of the edited range.
There have being reports where the "getActive..." methods don't return the correct objects i.e. when there is a delay on the execution of the function called by the on edit trigger, so, in order to prevent this, use the event object properties instead of those methods.
Related