使用Google Apps脚本填充特定单元格时,如何自动填充当前时间和日期

时间:2019-10-14 08:01:09

标签: javascript google-apps-script google-sheets

我希望在日期和时间在两个不同的单元格中填充,当它旁边的列中的单元格填充为“是”时。这个想法是创建结帐时间和日期。

脚本的顶部是签入,下半部分是签出,但是当前它仅用于签出时间,但是签出时间不起作用。有什么建议吗?

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);      
  } 
}

以下表格的屏幕快照是否有帮助。

enter image description here

1 个答案:

答案 0 :(得分:1)

由于细节很小,因此无法正常工作。列标题称为Check IN,在脚本中您正在寻找一个名为Check In的标题。因此,您只需要更改此内容即可:

var updateColName2 = "Check In";

对此:

var updateColName2 = "Check IN";

此外,如果仅在值Time In列为Date In时填充Check INYes列,则应添加一个新条件,因此可以改变这个:

if (dateCol2 > -1 && index2 > 1 && editColumn2 == updateCol2) {

收件人:

if (dateCol2 > -1 && index2 > 1 && editColumn2 == updateCol2 && actRng2.getValue() == "Yes") {

最后,您不需要定义headers2变量,它与headers相同。

编辑

此外,您应该在最后一个if块中添加以下行:

var date2T = Utilities.formatDate(new Date(), timezone, timestamp_formatT);
cell2T.setValue(date2T);

因此,完整的代码可能像这样:

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);
  } 
}

我希望这对您有帮助。