根据一个单元格中的更改修改Google电子表格中的时间戳 [英] Modify timestamp in google spreadsheet on the basis of changes in one cell

查看:157
本文介绍了根据一个单元格中的更改修改Google电子表格中的时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图修改在Google表格中修改特定单元格的时间戳。我的最终目标是,如果单元格被编辑为一个值,则修改的时间戳会反映出来,如果单元格的内容被完全删除,则时间戳也会被删除。我的当前脚本如下所示:

  function onEdit()
{
var s = SpreadsheetApp.getActiveSheet ();
if(s.getName()==Sheet1){
var r = s.getActiveCell();
if(r.getColumn()== 4){
var nextCell = r.offset(0,1); ((nextCell.getValue()==&& r.value!=)||(nextCell.getValue()!=&& r.value!=
) )){
nextCell.setValue(new Date());
} else if(nextCell.getValue()!=&& r.value ==){
nextCell.setValue();






$ b现在此代码能够将时间戳更改为当前对单元格的任何修改,但如果我完全删除单元格的内容,时间戳不会删除,它会再次更改为当前时间戳。

  function onEdit()$ b   $ b {
var s = SpreadsheetApp.getActiveSheet();
if(s.getName()==Sheet1)
{
var r = s.getActiveCell();
var rvalue = r.getValue();
if(r.getColumn()== 4)
{
var nextCell = r.offset(0,1);
if(rvalue!=))
{
nextCell.setValue(new Date());
}
else
{
nextCell.setValue();
}
}
}
}

您也可以这样做:

  function onEdit(e)
{
if(e .source.getActiveSheet()。getName()==Sheet1)
{
if(e.range.getColumn()== 4)
{
var nextCell = e.range.offset(0,1);
if(e.value!=))
{
nextCell.setValue(new Date());
}
else
{
nextCell.setValue();
}
}
}
}

后者更难调试。


注意:项目代码中只能有一个onEdit()。如果您有其他人,那么他们都需要与适当的条件逻辑合并成一个,以防止他们不适当地互动。



I have been trying to modify the timestamp when a specific cell is modified in google sheets. My end goal is that if the cell is edited to a value then the modified timestamp reflects and if contents of the cell is entirely deleted, then timestamp also deletes. My current script looks like this:

function onEdit()
{
  var s = SpreadsheetApp.getActiveSheet();
  if( s.getName() == "Sheet1" ) {
    var r = s.getActiveCell();
    if( r.getColumn() == 4 ) {
      var nextCell = r.offset(0,1);
      if((nextCell.getValue() == "" && r.value != "")  ||  (nextCell.getValue() != "" && r.value != "")) {
        nextCell.setValue(new Date());
      } else if(nextCell.getValue() != "" && r.value == "") { 
        nextCell.setValue("");
      }
    }
  }
}

Now this code is able to change the timestamp to current in case of any modification to cell, but if I entirely delete the content of the cell, timestamp does not delete, it again changes to the current timestamp.

解决方案

Try this:

function onEdit()
{
  var s = SpreadsheetApp.getActiveSheet();
  if( s.getName() == "Sheet1" ) 
  {
    var r = s.getActiveCell();
    var rvalue=r.getValue();
    if( r.getColumn() == 4 ) 
    {
      var nextCell = r.offset(0,1);
      if(rvalue != "")) 
      {
        nextCell.setValue(new Date());
      } 
      else
      { 
        nextCell.setValue("");
      }
    }
  }
}

You can also do it with something like this:

function onEdit(e)
{
  if(e.source.getActiveSheet().getName()=="Sheet1") 
  {
    if(e.range.getColumn()==4) 
    {
      var nextCell = e.range.offset(0,1);
      if(e.value!="")) 
      {
        nextCell.setValue(new Date());
      } 
      else
      { 
        nextCell.setValue("");
      }
    }
  }
}

Although the latter is a little more difficult to debug.

Note: there can only be one onEdit() in your project code. If you have others then they all need to be combined into one with the appropriate conditional logic to keep them from interacting inappropriately.

这篇关于根据一个单元格中的更改修改Google电子表格中的时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆