【问题标题】:How to add a '3 weeks from today' timestamp into a script that includes a 'today' timestamp?如何将“从今天起 3 周”时间戳添加到包含“今天”时间戳的脚本中?
【发布时间】:2015-04-23 20:00:35
【问题描述】:

每当我更新列 F(注释)时,我的脚本已经为我的列 H(调用时间)加上时间戳:

function onEdit(event)
{
  var timezone = "GMT-7";
  var timestamp_format = "MM-dd-yyyy HH:mm:ss"; // Timestamp Format.
  var updateColName = "Notes";
  var timeStampColName = "Call Time";
  var sheet = event.source.getSheetByName('Today'); //Name of the sheet      where you want to run this script.


  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) { // only     timestamp if 'Last Updated' header exists, but not in the header row itself!
    var cell = sheet.getRange(index, dateCol + 1);
    var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
    cell.setValue(date);
  }
}

现在我需要在我的列 F(CB 日期)上设置未来的时间戳(从现在起 3 周)。

这可能吗?

【问题讨论】:

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


    【解决方案1】:

    还有一些其他方面的代码可以改进一点,但要满足您的特定要求:

    function onEdit(event)
    {
      var timezone = "GMT-7";
      var timestamp_format = "MM-dd-yyyy HH:mm:ss"; // Timestamp Format.
      var updateColName = "Notes";
      var timeStampColName = "Call Time";
      var cbDateColName = "CB Date";
      var sheet = event.source.getSheetByName('Today'); //Name of the sheet      where you want to run this script.
    
      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) { // only     timestamp if 'Last Updated' header exists, but not in the header row itself!
        var cell = sheet.getRange(index, dateCol + 1);
        var cbcell = sheet.getRange(index, headers[0].indexOf(cbDateColName) + 1);
        var d = new Date();
        var cbd = new Date(d.getTime() + 3*7*24*60*60*1000);
        var date = Utilities.formatDate(d, timezone, timestamp_format);
        var cbdate = Utilities.formatDate(cbd, timezone, timestamp_format);
        cell.setValue(date);
        cbcell.setValue(cbdate);
      }
    }
    

    【讨论】:

    • 哇!谢谢!您的脚本完全符合我的要求。惊人的!!非常感谢您花时间为我澄清这一点。
    猜你喜欢
    • 2020-10-21
    • 1970-01-01
    • 1970-01-01
    • 2014-10-18
    • 1970-01-01
    • 1970-01-01
    • 2011-08-12
    • 2011-06-13
    • 1970-01-01
    相关资源
    最近更新 更多