I am currently working on a Project where I am trying to fill in the 'Last Updated' date based on changes made in a google sheet by a user.
I have three google sheets in the same workbook.
I followed a tutorial which helped me get what I wanted however, when I use the same code across sheets it uses the last one which I read OnEdit does.
As you can see below. If a user updates Column (Col) 1-17 in Sheet 1 then add the last updated to Col 18 in Row.
Note: The code below is all in 1 .gs file.
Expected Behaviour:
When I am in Sheet 1 update Col 18 (Last Update) based on changes and the ranges specified in Sheet1. If in Sheet 2 do the corresponding for the respective ranges there. Same for Sheet 3.
See Code Below:
function onEdit(x) {
addTimeStampGlossary(x);
}
function onEdit(y) {
addTimeStampTables(y);
}
function onEdit(z) {
addTimeStampFields(z);
}
function addTimeStampGlossary(x){
// variables
var startRow = 2;
var ws = "Sheet1";
//get modified row and column
var row = x.range.getRow();
var col = x.range.getColumn();
if(col === 1 || col === 2 || col === 3 || col === 4 || col === 5 || col === 6 || col === 7 || col === 8 || col === 9 || col === 10 || col === 11 || col === 12 || col === 13 || col === 14 || col === 15 || col === 16 || col === 17 && row >= startRow && x.source.getActiveSheet().getName() === ws ){
x.source.getActiveSheet().getRange(row,18).setValue(new Date());
}
}
function addTimeStampTables(y){
// variables
var startRow = 2;
var ws = "Sheet2";
//get modified row and column
var row = y.range.getRow();
var col = y.range.getColumn();
if(col === 1 || col === 2 || col === 3 || col === 4 || col === 5 || col === 6 || col === 7 || col === 8 || col === 9 || col === 10 || col === 11 || col === 12 || col === 13 || col === 14 || col === 15 || col === 16 || col === 17 || col === 18 || col === 19 && row >= startRow && y.source.getActiveSheet().getName() === ws ){
y.source.getActiveSheet().getRange(row,20).setValue(new Date());
}
}
function addTimeStampFields(z){
// variables
var startRow = 2;
var ws = "Sheet3";
//get modified row and column
var row = z.range.getRow();
var col = z.range.getColumn();
if(col === 1 || col === 2 || col === 3 || col === 4 || col === 5 || col === 6 || col === 7 || col === 8 || col === 9 || col === 10 || col === 11 || col === 12 || col === 13 || col === 14 || col === 15 || col === 16 && row >= startRow && z.source.getActiveSheet().getName() === ws ){
z.source.getActiveSheet().getRange(row,17).setValue(new Date());
}
}