[英]How do I auto populate the current time and date when a certain cell is populated using Google Apps Script
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
. header 列称为
Check IN
,在脚本中您正在寻找一个称为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:此外,如果您希望仅在值
Check IN
列是Yes
时填充Time In
和Date In
列,您应该添加一个新条件,以便您可以更改:
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
.最后,您不需要定义
headers2
变量,它与headers
相同。
Also, you should add this line inside the last if
block:此外,您应该在最后一个
if
块中添加这一行:
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.我希望这对你有任何帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.