Can you please help me why its not working everytime i click on button its not updating. I want to get the old value of column J and column K and set it to column L and M everytime it was change. Please help me TIA.
function submitValues() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName("Sheet1");
var sheet2 = ss.getSheetByName("Sheet2");
// Get the value from C11 on Sheet1
var cellC11 = sheet1.getRange("C11").getValue();
// Get the current date and time
var currentDate = new Date();
// Set the value of C11 from Sheet1 to Sheet2 in column J and set a new date in column K
sheet2.getRange("J1").setValue(cellC11);
sheet2.getRange("K1").setValue(currentDate);
}
function onEdit(e) {
var sheet = e.source.getSheetByName("Sheet2");
var range = e.range;
if (sheet.getName() === "Sheet2") {
if (range.getColumn() === 10) { // Column J is column 10
var oldValue = e.oldValue;
var newValue = e.value;
if (oldValue !== newValue) {
// Set the previous value in column L
sheet.getRange(range.getRow(), 12).setValue(oldValue);
}
} else if (range.getColumn() === 11) { // Column K is column 11
var oldDate = e.oldValue;
var newDate = e.value;
if (oldDate !== newDate) {
// Set the previous date in column M
sheet.getRange(range.getRow(), 13).setValue(oldDate);
}
}
}
}
I try to get the previous value of column J and Column K and set value to Column L and M. it only works when i manually type the value on the column J.
2
Answers
Try this approach. I found it from here. It takes the 5 digit number from the spreadsheet and converts it into the appropriate timestamp for Javascript.