如何使一个单元格连续显示该行中最近编辑的日期

时间:2019-02-28 16:47:36

标签: google-sheets timestamp formula

我正在Google表格中研究将要定期访问和编辑的文档。我希望有一个标记为“上次编辑日期”的列,并且该列中的每个单元格都能反映出每个单元格行最近编辑的日期。例如,如果我在单元格C3中编辑信息,那么我希望“上次编辑日期”列3中的单元格反映我进行编辑的日期。我发现有一些按列运行,但它们仅适用于单个列。我需要它覆盖“最后一次编辑日期”之前的所有行。

2 个答案:

答案 0 :(得分:0)

如果在“数据输入”选项卡上编辑了A:B,则此代码将使用用户的电子邮件地址更新C:C,并使用同一行的当前时间更新D:D。您需要设置一个onChange()触发器才能使其正常工作。

function userUpdate() {
    /* 
    Changed the updated by/on to make it faster
    Put in handling to make sure that if they delete a cell, that it only clears out the submitted by/on if ALL the values in A:B are blank
    */

    // Main sheet details
    var s = SpreadsheetApp.getActiveSheet();
    var spreadsheetTimeZone = SpreadsheetApp.getActive().getSpreadsheetTimeZone();
    var lastUpdatedString = Utilities.formatDate(new Date(), spreadsheetTimeZone, "MM/dd/yyyy' 'HH:mm:ss");
    var sessionEmail = Session.getActiveUser().getEmail();

    // Check Sheet
    if (s.getName() == "Data Entry") { //checks that we're on the correct sheet
        var r = s.getActiveCell();
        var row = r.getRowIndex();
        var column = r.getColumn();
        var activeRange = s.getRange("A" + row + ":B" + row);
        //var activeRangeValues = 
        var activeRangeValuesLength = activeRange.getValues().toString().length;

        if (row > 1) {

            // Declare variables

            var cellValue = r.getValue().toString();

            // If the cellValue IS blank
            if (activeRangeValuesLength < 5) {
                sessionEmail = "";
                lastUpdatedString = "";
            }

            //  Update Submitted By / Submitted On
            if (column < 5) {
              var lastUpdatedBy = s.getRange("C" + row)
              lastUpdatedBy.setValue(sessionEmail); // update column C 

              var lastUpdated = s.getRange("D" + row)
              lastUpdated.setValue(lastUpdatedString); // update column D    

            }
        }
    }

答案 1 :(得分:0)

将此脚本添加到工作表中,并根据需要更改格式和时区("GMT+1", "dd.MM.yyyy"):

function onEdit(event)
{ 

  var sheet = event.source.getActiveSheet();

  // note: actRng = the cell being updated
  var actRng = event.source.getActiveRange();
  var index = actRng.getRowIndex();
  var cindex = actRng.getColumnIndex();

  var dateCol = sheet.getLastColumn();  
  var lastCell = sheet.getRange(index,dateCol);
  var date = Utilities.formatDate(new Date(), "GMT+1", "dd.MM.yyyy");

  lastCell.setValue(date);
}