The script already is working properly upon testing. Maybe there is something that interferes with your trigger which can be caused by the first function. Thus you will need to merge them.
I renamed it to onEdit(e)
instead. I merged them since they are actually a subset of onEdit(e)
, just having different conditions. It should be fine to merge them under the same function.
Code:
var currentSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
function onEdit(e) {
if (!e) return;
var currentRange = currentSheet.getActiveRange();
var currentRow = currentRange.getRow();
if ( e.value &&
currentSheet.getName() == "Open Actions - Cutover Punchlist" , "Open Actions - FSA Interfaces" , "Open Actions - General" &&
currentRow > 2 ) {
if (currentRange.getColumn() == 8) { // adjacent column (first function conversion, if H column is edited)
var adjacentCell = currentSheet.getRange('H' + currentRow);
var timestampCell = adjacentCell.offset(0, 1);
timestampCell.setValue(new Date());
// since H is edited, timestamp column is updated
// so we copy (regardless if the old value is blank or a timestamp)
copyUpdates(currentRow);
}
if (currentRange.getColumn() == 9) { // timestamp column (second function conversion, if I column is edited)
// edited timestamp manually, copy
copyUpdates(currentRow);
}
}
}
function copyUpdates(currentRow) {
var dataRange = currentSheet.getRange(currentRow + ':' + currentRow);
var destinationSheet = currentSheet.getParent().getSheetByName("RecentUpdates");
var destinationRow = destinationSheet.getLastRow() + 1;
dataRange.copyTo(destinationSheet.getRange(destinationRow, 1), {
contentsOnly: true
});
}
Sample Data:
Sample Testing:
1. Wrote "add timestamp" to "H3" (Should trigger your first function)
2. Wrote "add timestamp" to "H4" (Should trigger your first function)
3. Edited "H4" to "change timestamp" (Should trigger your first function)
4. Edited "I4" to "1/22/2021" (Should trigger your second function)
Sample Data outcome:
RecentUpdates outcome:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…