I have a google sheets document in which I need to run the same script separetely for each sheet, this script place a timestamp on column A whenever a cell is filled on column B
function onEdit(event)
{
var timezone = "GMT-6";
var timestamp_format = "MM-dd-yyyy HH:mm:ss"; // Timestamp
var updateColName = "ColumnB"; //Columna que es actualiza
var timeStampColName = "ColumnA"; //Columna actualizada automáticamente
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1'); //Sheet
var actRng = SpreadsheetApp.getActiveSpreadsheet().getActiveRange();
var editColumn = actRng.getColumn();
var index = actRng.getRowIndex();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var dateCol = headers[0].indexOf(timeStampColName);
var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
if (dateCol > -1 && index > 1 && editColumn == updateCol) { // only timestamp if 'Last Updated' header exists, but not in the header row itself!
var cell = sheet.getRange(index, dateCol + 1);
var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
cell.setValue(date);
}
}
When I write another script for columnA, columnB and Sheet2, only puts the timestamp in sheet1, even when I write something on columnB from Sheet2.
Any suggestions?
One of the suggestions was this:
function onEdit(event)
{
var timezone = "GMT-6";
var timestamp_format = "MM-dd-yyyy HH:mm:ss"; // Timestamp
var updateColName = "Timestamp"; //Columna que es actualiza
var timeStampColName = "Ticket"; //Columna actualizada automáticamente
var sheet = event.source.getActiveSheet();
var actRng = event.source.getActiveRange();
var editColumn = actRng.getColumn();
var index = actRng.getRowIndex();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var dateCol = headers[0].indexOf(timeStampColName);
var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
if (dateCol > -1 && index > 1 && editColumn == updateCol) { // only timestamp if 'Last Updated' header exists, but not in the header row itself!
var cell = sheet.getRange(index, dateCol + 1);
var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
cell.setValue(date);
}
}
But it does not place the timestamp on the columnA.