简体   繁体   English

如何在使用 Google Apps 脚本填充某个单元格时自动填充当前时间和日期

[英]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 InDate 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相同。

EDIT编辑

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.

相关问题 我怎样才能让一个单元格在编辑时发布它旁边的编辑日期? 在 Google 表格中使用 Google Apps 脚本 - How can I have a cell that when edited publishes it's edit date next to it? In Google Sheets using a Google Apps Script 如何在Google Apps脚本中获取“this”文件的当前实例? - How do I get the current instance of “this” file in Google Apps Script? 如何使用应用程序脚本在单元格中显示日期 - How to display date in a cell using apps script 如何让谷歌脚本识别单元格中的日期? - How do I get google Script to recognize a date in a cell? 如何使用谷歌应用程序脚本在 gsheets 中自动填充公式 - How do I make formula auto populate in gsheets with google app script 使用javascript或jquery自动填充当前时间 - Using javascript or jquery to auto populate the current time 如何在 Google Apps 脚本中自动回复指定的电子邮件 - How do I omit specified emails from being auto replied to in a Google Apps Script 如何在 Google Apps 电子表格脚本中将日期和时间合并为日期时间? - How to merge date and time as a datetime in Google Apps Spreadsheet script? 在 Google 表格中使用 Google Apps 脚本时如何防止日期变成字符串? - How to keep the date from turning into a String when using a Google Apps Script in Google Sheets? 在编辑其表格行时,如何自动将Google表格单元格设置为当前日期? - How do I automatically set a Google Sheets cell to the current date on editing its row?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM