Google脚本 - 在特定单元格中添加今天日期,但如果过度输入则忽略更新单元格

时间:2017-05-09 20:09:36

标签: google-sheets

所以我试图完成以下任务。

每当我在A列中输入内容时......今天的日期将进入O列(同一行)。我已经有了这个代码,但我需要稍微修改它。

  function onEdit(e) {
  var sheet      = e.source.getActiveSheet();
  var activeCell = sheet.getActiveCell();
  var col        = activeCell.getColumn();
  var row        = activeCell.getRow();


// If the Reference is put into Column A, then todays date is automatically put into Column O

  if (col == 1 && sheet.getRange(row, col) != "") { // 1 = Column A
      sheet.getRange(row, col+14).setValue(Utilities.formatDate(new Date(), "GMT+00:00", "''dd-MM-yyyy")); // +14 = Column O
  }

}

所以这就是我需要它做的事情。 但我想要完成的修改是:

如果A列中的值因任何原因(例如明天)更新,那么O列中的日期也将更新。我不希望这种情况发生。我希望日期保持不变。

这可能吗?

我不妨问一下我一直在思考的另一个问题。

我不太喜欢A列的整个“col == 1”和O列的“col + 14”。 有没有办法可以搜索列标题?并动态地使用它。

列A标题是:参考编号

列O标题是:收到日期

我熟悉Excel VBA,我知道您可以使用该语言执行此类操作,但我不知道如何在Google Scripts中执行此操作。

对此有任何帮助将非常感激。

谢谢

1 个答案:

答案 0 :(得分:0)

确保您不会覆盖该值。您只需修改if语句以检查单元格的值是否为空

if (col == 1 &&                                    // check if the column no is 1
  sheet.getRange(row, col).getValue() != "" &&     // Check if the new edited value is not empty
  sheet.getRange(row, col + 14).getValue() == "") {  // check if col 14 is empty
  sheet.getRange(row, col + 14).setValue(Utilities.formatDate(new Date(), "GMT+00:00", "''dd-MM-yyyy")); // +14 = Column O
}

要根据标题中的特定字符串获取列号,请先获取第一行中的值。然后使用indexOf()函数来确定哪个数组元素具有该特定字符串,(添加1,因为数组索引从0开始,电子表格列号从1开始)

var headers = sheet.getRange(1,1,1,sheet.getLastColumn()).getValues()[0]   //Get the first row values in a 2D array, then get the first row (0)

  //find the columns with following headers
  var headerFind = ["Reference Number","Date Received"]
  // column numbers with the given headers
  var col1 = headers.indexOf(headerFind[0]) + 1
  var col2 = headers.indexOf(headerFind[1]) + 1
  Logger.log(col1)
  Logger.log(col2)

最终代码是这样的:

function onEdit(e) {
  var sheet      = e.source.getActiveSheet();
  var activeCell = sheet.getActiveCell();
  var col        = activeCell.getColumn();
  var row        = activeCell.getRow();
  var headers = sheet.getRange(1,1,1,sheet.getLastColumn()).getValues()[0]   //Get the first row values in a 2D array, then get the first row (0)

  //find the columns with following headers
  var headerFind = ["Reference Number","Date Received"]
  // column numbers with the given headers
  var col1 = headers.indexOf(headerFind[0]) + 1
  var col2 = headers.indexOf(headerFind[1]) + 1
  Logger.log(col1)
  Logger.log(col2)

// If the Reference is put into Column A, then todays date is automatically put into Column O

  if (col == col1 &&                                    // check if the column no is 1
      sheet.getRange(row, col).getValue() != "" &&     // Check if the new edited value is not empty
      sheet.getRange(row, col2).getValue() == "") {  // check if col2 is empty
      sheet.getRange(row, col2).setValue(Utilities.formatDate(new Date(), "GMT+00:00", "''dd-MM-yyyy")); // +14 = Column O
    }
}
相关问题