【问题标题】:Need time stamps for two separate sheets in google spreadsheet within same custom script需要同一自定义脚本中谷歌电子表格中两个单独工作表的时间戳
【发布时间】:2017-06-29 15:37:16
【问题描述】:

我在 Google 表格中有一个文档(我从事医疗保健模拟工作),该文档在主文件中有两个单独的表格(选项卡)。一个标签是 MAR,另一个是 VITALS。我需要每个都从同一个脚本中运行,并且不会相互干扰。我只能让一个工作,另一个不行。

我将在下面发布每一个(您将能够看到,当一个工作时,另一个不工作,但当另一个//rem'd out 时,每个都将工作)。

我不确定要更改哪些变量才能使它们共存并协同工作。我知道我一定错过了一些简单的东西。 谢谢您提前!

三月时间戳

function onEdit() {
  var x = SpreadsheetApp.getActiveSheet();
  if( x.getName() == "MAR" ) { //checks that we're on the correct sheet
    var y = x.getActiveCell();
    if( y.getColumn() == 4 ) { //checks the column
      var nextCell = y.offset(0,1);
      if( nextCell.getValue() === '' ) //is empty?
        nextCell.setValue(new Date());
    }
  }
}

重要的时间戳

function onEdit() {
  var x = SpreadsheetApp.getActiveSheet();
  if( x.getName() == "Vitals" ) { //checks that we're on the correct sheet
    var y = x.getActiveCell();
    if( y.getColumn() == 1 ) { //checks the column
      var nextCell = y.offset(0,1);
      if( nextCell.getValue() === '' ) //is empty?
        nextCell.setValue(new Date());
    }
  }
}

【问题讨论】:

    标签: date time google-sheets


    【解决方案1】:

    这个怎么样?

     function onEdit() {
      var x = SpreadsheetApp.getActiveSheet();
      if( x.getName() == "MAR" ) { //checks that we're on the correct sheet
        var y = x.getActiveCell();
        if( y.getColumn() == 4 ) { //checks the column
          var nextCell = y.offset(0,1);
          if( nextCell.getValue() === '' ) //is empty?
            nextCell.setValue(new Date());
        }
      }
    if( x.getName() == "Vitals" ) { //checks that we're on the correct sheet
        var y = x.getActiveCell();
        if( y.getColumn() == 1 ) { //checks the column
          var nextCell = y.offset(0,1);
          if( nextCell.getValue() === '' ) //is empty?
            nextCell.setValue(new Date());
        }
      }
    }
    

    【讨论】:

    • 您先生是个铁石心肠的导弹人!我非常感谢你和这里的其他人!
    【解决方案2】:

    或者……

    function onEdit(e) {
    var sh, sheets, cols, sheetInd, o;
    sh = e.source.getActiveSheet()
    sheets = ["MAR", "Vitals"]
    cols = [4, 1]
    sheetInd = sheets.indexOf(sh.getName())
    if(sheetInd == -1 || cols[sheetInd !== e.range.columnStart]) return;
    o = e.range.offset(0, 1)
    if(!o.getValue()) o.setValue(new Date())
    }
    

    【讨论】:

    • 最初的答案很完美,但是如果我尝试运行这个脚本,我会收到这个错误:TypeError: Cannot read property "source" from undefined。 (第 3 行,文件“DateTimeStamps”)...是否存在使用一个脚本而不是另一个脚本的潜在陷阱?我只是想提前考虑...谢谢您的帮助!
    • 此脚本使用在编辑电子表格时“形成”的事件对象。您不能手动运行此脚本(通过单击脚本编辑器中的“播放”按钮。相反,您必须返回工作表并编辑指定的列。然后查看日期是否出现。
    • 现在阅读...谢谢!我想尽我所能地学习 GS/JS,但我不知道从哪里开始。你们真的是救生员
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    相关资源
    最近更新 更多