I have done a script of google sheet
that writes me the modification date of a specific range of cells and now I want to make it detect the cell that I am modifying and detect if it is within a few ranges concrete.
This is the script that I have:
function onEdit(HoraModificacion){
var DateCol = "K9";
var curDate = Utilities.formatDate(new Date(), "GMT+01:00", "hh:mm");
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Test2');
var range = sheet.getRange('C11:K109');
var rowIndex = range.getRowIndex();
var colIndex = range.getColumnIndex();
// May need to set up multiple ranges like this and check all ranges if
// checked fields are not in adjacent cells
var watchRange = {
top : 11, // start row
bottom : 109, // end row
left : 2, // start col
right : 12, // end col
};
// Exit if we're out of range
if (rowIndex < watchRange.top || rowIndex > watchRange.bottom) return;
if (colIndex < watchRange.left || colIndex > watchRange.right) return;
var currentValue = range.getValue();
currentValue = currentValue.replace(/^\s+|\s+$/g,""); //"trim" kludge
if (currentValue.length != 0){
// Set a column to show last edit date
SpreadsheetApp.getActiveSheet().getRange(DateCol).setValue(curDate);
};
}
The problem is that I do not know how to detect the cell that I am modifying and from this it detects the range of some previously selected, I had planned to do something like the one below, but it does not finish me. The purpose of this is to have a series of controlled ranges, to know when I have modified them, each range has its own cell to write the date of the modification.
function onEdit(HoraModificacion){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Test2');
//Get the cell you are modifying to make a conditional for multiple range detect
var activeCell = sheet.getActiveCell();
//var activeRange = sheet.getActiveRange();
var range1 = sheet.getRange('C11:K109');
if ( activeCell == range1){
var DateCol = "K9";
var curDate = Utilities.formatDate(new Date(), "GMT+01:00", "hh:mm");
var rowIndex = range.getRowIndex();
var colIndex = range.getColumnIndex();
// May need to set up multiple ranges like this and check all ranges if
// checked fields are not in adjacent cells
var watchRange = {
top : 11, // start row
bottom : 109, // end row
left : 2, // start col
right : 12, // end col
};
// Exit if we're out of range
if (rowIndex < watchRange.top || rowIndex > watchRange.bottom) return;
if (colIndex < watchRange.left || colIndex > watchRange.right) return;
var currentValue = range.getValue();
currentValue = currentValue.replace(/^\s+|\s+$/g,""); //"trim" kludge
if (currentValue.length != 0){
// Set a column to show last edit date
SpreadsheetApp.getActiveSheet().getRange(DateCol).setValue(curDate);
};
}else if{
//Check the other range
return;
};
}
Based on what Ruben told me, I modified my script and now I have this:
function onEdit(e){
var curDate = Utilities.formatDate(new Date(), "GMT+01:00", "hh:mm");
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Test2');
var colInicio = e.range.getColumn();
var filaInicio = e.range.getRow();
var ref = e.range.getA1Notation(); // Notación tipo A1 o A1:B2
var range = e.getRange();
var colIndex = colInicio.getColIndex();
var rowIndex = filaInicio.getRowIndex();
var watchRange1 = {
top : 11, // start row
bottom : 109, // end row
left : 3, // start col
right : 11, // end col
};
var watchRange2 = {
top : 11, // start row
bottom : 109, // end row
left : 13, // start col
right : 21, // end col
};
if(rowIndex >= watchRange1.top && rowIndex <= watchRange1.bottom &&
colIndex >= watchRange1.left && colIndex <= watchRange1.rigth &&
range.getValue() != 0){
var DateCol1 = "K9";
SpreadsheetApp.getActiveSheet().getRange(DateCol1).setValue(curDate);
}else if (rowIndex >= watchRange2.top && rowIndex <= watchRange2.bottom &&
colIndex >= watchRange2.left && colIndex <= watchRange2.rigth &&
range.getValue() != 0){
var DateCol2 = "U9";
SpreadsheetApp.getActiveSheet().getRange(DateCol2).setValue(curDate);
}else{
//if test
var DateCol1 = "K9";
SpreadsheetApp.getActiveSheet().getRange(DateCol1).setValue("Fallo");
}
}
When trying to execute this script I get this error: Unable to read the "range" property of undefined. (line 4, file "Dia1")
I'm still pretty stuck with the script
I detect that the problem is before if, but I can not solve it.