提示:其它语言是由 Google 机器翻译的。 你可以访问 English 版本。
登录
x
or
x
x
马上登记
x

or

如何在Google表格的另一列更新数据时自动插入时间戳?

如果您有一定范围的单元格,并且您希望在另一列中修改或更新数据时将时间戳自动插入相邻单元格中。 你怎么能在Google工作表中解决这个问题?

使用脚本代码在另一列更新数据时自动插入时间戳


使用脚本代码在另一列更新数据时自动插入时间戳


以下脚本代码可以帮助您快速轻松地完成这项工作,请按照以下步骤操作:

1。 点击 > 脚本编辑器,看截图:

2。 在打开的项目窗口中,请复制并粘贴以下脚本代码以替换原始代码,请参阅截图:

function onEdit(e)
{ 
  var sheet = e.source.getActiveSheet();
  if (sheet.getName() == "order data") //"order data" is the name of the sheet where you want to run this script.
  {
    var actRng = sheet.getActiveRange();
    var editColumn = actRng.getColumn();
    var rowIndex = actRng.getRowIndex();
    var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
    var dateCol = headers[0].indexOf("Date") + 1;
    var orderCol = headers[0].indexOf("Order") + 1;
    if (dateCol > 0 && rowIndex > 1 && editColumn == orderCol) 
    { 
      sheet.getRange(rowIndex, dateCol).setValue(Utilities.formatDate(new Date(), "UTC+8", "MM-dd-yyyy")); 
    }
  }
}

注意:在上面的代码中, 订单数据 是您想要使用的工作表名称, 日期 是要插入时间戳的列标题,而 关于产品订购 是要更新单元值的列标题。 请将它们更改为您的需要。

3。 然后保存项目窗口,然后输入这个新项目的名称,请参见截图:

4。 然后返回表单,现在,当“订单”列中的数据被修改时,当前时间戳会自动插入日期列的单元格中,并与修改的单元格相邻,请参阅屏幕截图:


Kutools for Excel解决了您的大多数问题,并使您的生产率提高了80%

  • 重用: 快速插入 复杂的公式,图表 以及你以前用过的任何东西; 加密单元格 密码; 创建邮件列表 并发送电子邮件...
  • 超级方程式酒吧 (轻松编辑多行文字和公式); 阅读布局 (轻松读取和编辑大量单元格); 粘贴到过滤范围...
  • 合并单元格/行/列 不丢失数据; 分裂细胞含量; 组合重复的行/列...防止重复的细胞; 比较范围...
  • 选择复制或唯一 行; 选择空行 (所有细胞都是空的); 超级查找和模糊查找 在许多工作簿中; 随机选择......
  • 精确复制 多个单元格而不更改公式参考; 自动创建参考 多张表; 插入项目符号,复选框等等......
  • 提取文本,添加文本,按位置删除, 删除空间; 创建和打印分页小计; 在单元格内容和注释之间转换...
  • 超级过滤器 (将过滤方案保存并应用到其他工作表); 高级排序 按月/周/日,频率等; 特殊过滤器 用粗体,斜体......
  • 结合工作簿和工作表; 根据键列合并表; 将数据拆分为多个表格; 批量转换xls,xlsx和PDF...
  • 超过300强大的功能。 支持Office / Excel 2007-2019和365。 支持所有语言。 在您的企业或组织中轻松部署。 全功能30天免费试用。
kte tab 201905

Office选项卡为Office提供选项卡式界面,使您的工作更轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,Publisher,Access,Visio和Project。
  • 在同一窗口的新选项卡中打开并创建多个文档,而不是在新窗口中。
  • 通过50%提高您的工作效率,每天为您减少数百次鼠标点击!
官方底部
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    eddie · 1 months ago
    hi when i run this script it give me a error on line 3 ? can someone help me get this fix please
    • To post as a guest, your comment is unpublished.
      Carlotta · 1 months ago
      Same problem here.
    • To post as a guest, your comment is unpublished.
      skyyang · 1 months ago
      Hello, eddie,
      Could you change the sheet name in the above code to your own sheet?
      Please check it, thank you!
      • To post as a guest, your comment is unpublished.
        eddie · 1 months ago
        function onEdit()
        {
        var sheet = SpreadsheetApp.getActiveSheet();
        var capture = sheet.getActiveCell();
        if (sheet.getName() == "Jewel Inbound trailer") //"order data" is the name of the sheet where you want to run this script.
        {
        var actRng = sheet.getActiveRange();
        var editColumn = actRng.getColumn();
        var rowIndex = actRng.getRowIndex();
        var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
        var dateCol = headers[0].indexOf("Time") + 1;
        var orderCol = headers[0].indexOf("Tractor") + 1;
        if (dateCol > 0 && rowIndex > 1 && editColumn == orderCol)
        {
        sheet.getRange(rowIndex, dateCol).setValue(Utilities.formatDate(new Date(), "UTC-6", "MM-dd-yyyy"));
        }
        }
        }
        • To post as a guest, your comment is unpublished.
          eddie · 1 months ago
          can someone help me to make this work correctly on google sheets


          thank you
          • To post as a guest, your comment is unpublished.
            Javier · 1 months ago
            It didn't work for me
          • To post as a guest, your comment is unpublished.
            skyyang · 1 months ago
            Hello, eddie,
            I have tried your code, it works well in my Google sheet, please try it again! Thank you!
  • To post as a guest, your comment is unpublished.
    isrami · 2 months ago
    can we change this to track changes on certain range of column instead of column? assuming that our column to be tracked is at the middle of our sheet?
  • To post as a guest, your comment is unpublished.
    Sarah · 2 months ago
    How do you track changes on more than one column though? Using your example, how do you edit the script to track changes in both "product" and "order" columns?
  • To post as a guest, your comment is unpublished.
    Simón Blanco · 3 months ago
    Existe una manera de hacer esto pero que la fecha se introduzca sólo si se escribe una palabra específica?
  • To post as a guest, your comment is unpublished.
    Ricardo · 3 months ago
    Genial, excelente, es lo que estaba buscando, muchas gracias, saludos
  • To post as a guest, your comment is unpublished.
    Juan Hernnandez · 4 months ago
    Awesome! Thanks
  • To post as a guest, your comment is unpublished.
    Daniel Méndez · 6 months ago
    Hola, hice los pasos que mencionas pero me aparece un error: TypeError: No se puede leer la propiedad "source" de undefined. (línea 3, archivo "Código")
    • To post as a guest, your comment is unpublished.
      Fabricio Rodrigues · 5 months ago
      I fix it whit this code.


      function onEdit() {
      var sheet = SpreadsheetApp.getActiveSheet();
      var capture = sheet.getActiveCell();
      if (sheet.getName() == "Updates") //"Updates" is the sheet name.
      if(capture.getColumn() == 1 ) {
      var add = capture.offset(0, 1); //"0" is the line in reference the cell updated, ''0'' same line, "1" reference at column "1" is 1 column to the right.
      var data = new Date();
      data = Utilities.formatDate(data, "GMT-03:00","dd/MM' 'HH:mm' '");
      add.setValue(data);
      }

      }
      • To post as a guest, your comment is unpublished.
        Jorge · 5 months ago
        Hi Fabricio!

        On the 1 I have to write the Date column (where I want to get the date) and on 0 column where I write text?
        Do I need "" or similar?

        Thanks!
        • To post as a guest, your comment is unpublished.
          Fabricio Rodrigues · 5 months ago
          Hello Jorge, no, you just need to write the number referente to column, like A = 1 , B = 2 .....
  • To post as a guest, your comment is unpublished.
    Rej · 6 months ago
    Good day! I'm just wondering if it's possible to add a code for the timestamp to automatically disappear once the main cell has been cleared. Thank!
  • To post as a guest, your comment is unpublished.
    ScottC · 7 months ago
    How should the script be modified to look for changes in a contiguous range of columns rather than a single column? e.g. trigger the script if there are changes in columns labeled, "Amount", "Category" and "Type" rather than the single column labeled "Order" in the example script.
  • To post as a guest, your comment is unpublished.
    Annette · 9 months ago
    Hey! I got this code "Missing } after function body. (line 18, file "Code")" How do I fix this issue? Thank you so much! This is amazing!
  • To post as a guest, your comment is unpublished.
    James · 11 months ago
    Hi
    I got the code working, thanks!
    If I would like to include mutiple columns, how would I alter the code?
    • To post as a guest, your comment is unpublished.
      Blaze · 9 months ago
      I am trying to do the same, any luck figuring this out?
  • To post as a guest, your comment is unpublished.
    Nicky · 1 years ago
    Hi I face an error TypeError: Cannot read property "source" from undefined. (line 3, file "Code")
    Able to help on this
  • To post as a guest, your comment is unpublished.
    Willy · 1 years ago
    Thanks for this code, it's exactly what I need. The only problem is I am running a script that sends some data to google sheet, but the time stamp doesn't trigger for this data, only when I edit the cell manually. Any advice?
  • To post as a guest, your comment is unpublished.
    Ryan · 1 years ago
    I am getting an error "TypeError: Cannot read property "source" from undefined. (line 3, file "Code"). Do I have to provide the link of the sheet in this line?


    thanks,


    Ryan
  • To post as a guest, your comment is unpublished.
    Kurtis Lipman · 1 years ago
    HI there,


    I'm looking to do the equivalent get a timestamp in the "date" column whenever the "Order" is updated, but also whenever the "Delivery Status" or "Payment Status" is updated as well (making up column heading but I hope you get my drift).

    Is this possible?


    Thanks
  • To post as a guest, your comment is unpublished.
    Mahdi · 1 years ago
    I love this script. How do I only get this to Print Time instead of DATE? That is what I need
    • To post as a guest, your comment is unpublished.
      skyyang · 1 years ago
      Hello,

      you also can apply the following code, but, you should change the time zone to your own. Please try it.

      function onEdit(e)
      {
      var sheet = e.source.getActiveSheet();
      if (sheet.getName() == "order data") //"order data" is the name of the sheet where you want to run this script.
      {
      var actRng = sheet.getActiveRange();
      var editColumn = actRng.getColumn();
      var rowIndex = actRng.getRowIndex();
      var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
      var dateCol = headers[0].indexOf("Date") + 1;
      var orderCol = headers[0].indexOf("Order") + 1;
      if (dateCol > 0 && rowIndex > 1 && editColumn == orderCol)
      {
      sheet.getRange(rowIndex, dateCol).setValue(Utilities.formatDate(new Date(), "GMT+8:00", "HH:mm:ss"));
      }
      }
      }
      • To post as a guest, your comment is unpublished.
        Scott Ratner · 1 years ago
        How do I make it have both Time and Date?


        Thanks.


        Scott
        • To post as a guest, your comment is unpublished.
          Some · 10 months ago
          You can simply add hh:mm:ss after the date in line 14 of the code (copied below). Note: I had to change the UTC+8 to GMT-5 to get it to stamp the correct time for US Eastern.

          sheet.getRange(rowIndex, dateCol).setValue(Utilities.formatDate(new Date(), "GMT-5", "MM-dd-yyyy hh:mm:ss"));
        • To post as a guest, your comment is unpublished.
          skyyang · 1 years ago
          Hi, Scott,

          To make the column have both date and time, you should apply the following script code. After inserting the code, and then select the column that you want to insert the date and time, then click Format > Number > Date time to format the cells as date time formatting.

          function onEdit(e)
          {
          var sheet = e.source.getActiveSheet();
          if (sheet.getName() == "order data") //"order data" is the name of the sheet where you want to run this script.
          {
          var actRng = sheet.getActiveRange();
          var editColumn = actRng.getColumn();
          var rowIndex = actRng.getRowIndex();
          var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
          var dateCol = headers[0].indexOf("Date") + 1;
          var orderCol = headers[0].indexOf("Order") + 1;
          if (dateCol > 0 && rowIndex > 1 && editColumn == orderCol)
          {
          sheet.getRange(rowIndex, dateCol).setValue(new Date());
          }
          }
          }

          Please try it, hope it can help you!
    • To post as a guest, your comment is unpublished.
      Basir · 1 years ago
      Change the last line to sheet.getRange(rowIndex, dateCol).setValue(new Date());
      This will return a date time, but you can show only the time if you want from Format -> Number -> Time
  • To post as a guest, your comment is unpublished.
    PatKat · 1 years ago
    Hi. Thanks for the solution. I have a shared file and I would like the time to be reflected when anyone edits the sheet. Currently, this works only when I edit the sheet. How do I do that? Thanks in advance :)
  • To post as a guest, your comment is unpublished.
    césar pereira · 1 years ago
    I also would like to know how to lock that cell after the information is inserted in the previous cell.
  • To post as a guest, your comment is unpublished.
    césar pereira · 1 years ago
    Hi there, thanks for the code it worked perfectly for what i needed. However I would need your help to know how to add a condition for this date to appear.
    In fact, I would like to have this date only when numbers are inserted and nothing else.
    Do you know what I should add to the code for that?
    I am not a coder at all, only a copy paster, this is why I really need help and can't figure it out by myself.
    thanks a lot already for your help

    cesar
  • To post as a guest, your comment is unpublished.
    Jeff Oxford · 1 years ago
    Do I need to run the function in the script editor for this to work? I keep getting this error when I try it: TypeError: Cannot read property "source" from undefined. (line 3, file "Code")
    • To post as a guest, your comment is unpublished.
      Nathan · 1 years ago
      Hi there!
      I had this issue too. It ended up being that I renamed my file to "order data", but my sheet name was still "Sheet1" once I renamed the sheet and not the workbook to "order data" everything worked.
  • To post as a guest, your comment is unpublished.
    David · 1 years ago
    can this be modified to apply to any sheet?