Reputation: 3
This may be a simple answer, I'm very new to the script editor in google sheets.
I have about 8 sheets for individual users to enter data. I would like a note showing the last modified date on a range of cells in each of the 8 sheets. Each of the 8 sheets is identical in the layout. With the code below, I'm able to get a last modified date note on column 2, row 26, and it works great. However, I can't figure out how to expand that to include columns 2-7 and rows 26-35. I assumed just entering 2:7 under range.getRow() would work, but it does not.
Any suggestions on how to achieve this? Even better, ideally having just one comment note in a single cell on the sheet(L24 as an example), that shows the last modified date of any cell edited between columns 2-8 and rows 26-35?
My goal is really just to identify the last time a user has modified any cells within that specified range.
Any suggestions would be greatly appreciated. The script is below.
/**
* The event handler triggered when editing the spreadsheet.
* @param {Event} e The onEdit event.
*/
function onEdit(e) {
// Set a comment on the edited cell to indicate when it was changed.
var range = e.range;
if(range.getRow() == 26 && range.getColumn() == 2){
range.setNote('ROW: ' + range.getRow() + " COLUMN: "+ e.range.getColumn());
range.setNote('Last modified: ' + new Date());
}}
Upvotes: 0
Views: 605
Reputation: 8044
The OP wants to create a note in cell L24 on any sheet where the edited cell is in the range B26:H35 (columns 2 to 8 inclusive).
The OP's own code could create a note in the edited cell but was only configured for edits in the range "B26". However, most of the logic was in place, and only modest changes are required.
The points to note in this answer are:
noterange
. getA1Notation
. function so54658181(e) {
// setup spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
// get the active sheet
var eactiveSheet = e.source.getActiveSheet();
// set the location for the Note
var noterange = eactiveSheet.getRange("L24");
// get the edited cell and find the row and column numbers
var erange = e.range;
var editedRow = erange.getRow();
var editedColumn = erange.getColumn()
// if the row is between 26 and 35, and the column is between 2 and 8, then set the note
if ((editedRow >= 26 && editedRow <= 35) && (editedColumn >= 2 && editedColumn <= 8)) {
noterange.setNote("Cell " + erange.getA1Notation() + String.fromCharCode(10) + "Last modified: " + new Date());
}
}
Formatting the Date
Add these two line anywhere above the IF
statement
// set date format
var curDate = Utilities.formatDate(new Date(), "GMT+1", "E, MMM dd yyyy");
Edit the following line of the IF statement; note that it now references curDate
noterange.setNote("Latest edit: Cell " + erange.getA1Notation() + String.fromCharCode(10) + "On: " + curDate);
A link to the most recent Date and Time Patterns is included in the Google documentation.
Upvotes: 0