I'd like the date and time to populate in two different cells, when the cell in the column next to it is populated with 'Yes'. The idea is to create a checkout time and date.
The top portion of the script is check in, and the lower half is checkout, however currently it is just for check out time, but the check out time doesn't work. Any advise?
function onEdit(event) {
var timezone = "GMT+1";
var timestamp_format = "hh:mm:ss"; // Timestamp Format.
var timestamp_formatT = "dd-MM-yyyy";
var updateColName = "Student Name";
var timeStampColName = "Time Out";
var sheet = event.source.getSheetByName('2019/20');
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) {
var cell = sheet.getRange(index, dateCol + 1);
var cellT = sheet.getRange(index, dateCol + 2);
var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
var dateT = Utilities.formatDate(new Date(), timezone, timestamp_formatT);
cell.setValue(date);
cellT.setValue(dateT);
}
var updateColName2 = "Check In";
var timeStampColName2 = "Time In";
var actRng2 = event.source.getActiveRange();
var editColumn2 = actRng2.getColumn();
var index2 = actRng2.getRowIndex();
var headers2 = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var dateCol2 = headers2[0].indexOf(timeStampColName2);
var updateCol2 = headers2[0].indexOf(updateColName2);
updateCol2 = updateCol2 + 1;
if (dateCol2 > -1 && index2 > 1 && editColumn2 == updateCol2) {
var cell2 = sheet.getRange(index2, dateCol2 + 1);
var date2 = Utilities.formatDate(new Date(), timezone, timestamp_format);
cell2.setValue(date2);
}
}
Screen shot of the sheet below if it's any help.
It's not working because of a very small detail. The column header is called Check IN
and in the script you are looking for one called Check In
. So you just need to change this:
var updateColName2 = "Check In";
To this:
var updateColName2 = "Check IN";
Also, if you want the Time In
and Date In
columns to be populated only when the value Check IN
column is Yes
, you should add a new condition, so you could change this:
if (dateCol2 > -1 && index2 > 1 && editColumn2 == updateCol2) {
To:
if (dateCol2 > -1 && index2 > 1 && editColumn2 == updateCol2 && actRng2.getValue() == "Yes") {
Finally, you don't need to define a headers2
variable, it's the same as headers
.
Also, you should add this line inside the last if
block:
var date2T = Utilities.formatDate(new Date(), timezone, timestamp_formatT);
cell2T.setValue(date2T);
So, full code could be like:
function onEdit(event) {
var timezone = "GMT+1";
var timestamp_format = "hh:mm:ss"; // Timestamp Format.
var timestamp_formatT = "dd-MM-yyyy";
var updateColName = "Student Name";
var timeStampColName = "Time Out";
var sheet = event.source.getSheetByName('2019/20');
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) {
var cell = sheet.getRange(index, dateCol + 1);
var cellT = sheet.getRange(index, dateCol + 2);
var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
var dateT = Utilities.formatDate(new Date(), timezone, timestamp_formatT);
cell.setValue(date);
cellT.setValue(dateT);
}
var updateColName2 = "Check IN";
var timeStampColName2 = "Time In";
var actRng2 = event.source.getActiveRange();
var editColumn2 = actRng2.getColumn();
var index2 = actRng2.getRowIndex();
var dateCol2 = headers[0].indexOf(timeStampColName2);
var updateCol2 = headers[0].indexOf(updateColName2);
updateCol2 = updateCol2 + 1;
var editedValue = actRng2.getValue();
if (dateCol2 > -1 && index2 > 1 && editColumn2 == updateCol2 && editedValue == "Yes") {
var cell2 = sheet.getRange(index2, dateCol2 + 1);
var cell2T = sheet.getRange(index2, dateCol2 + 2);
var date2 = Utilities.formatDate(new Date(), timezone, timestamp_format);
var date2T = Utilities.formatDate(new Date(), timezone, timestamp_formatT);
cell2.setValue(date2);
cell2T.setValue(date2T);
}
}
I hope this is of any help to you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.