简体   繁体   English

阻止时间戳在应用程序脚本中更新

[英]Prevent timestamp from updating in apps script

I'm new to apps script and scripting in general. 我是应用脚本和脚本编写的新手。 That's why I signed up here (I've been reading for month now). 这就是为什么我在这里注册(我已经阅读了一个月)。

I need to make a Timestamp in a google sheet with apps script. 我需要使用应用程序脚本在Google工作表中创建一个时间戳。 My function checks, if any row in column B has a specific value (string) and if so, it prints a timestamp in column K in the same row. 我的函数检查B列中是否有任何特定值(字符串),如果是,则在同一行的K列中打印时间戳。

So: In B3 appears "Done" and the timestamp is printed in K3. 因此:在B3中出现“完成”,并且在K3中打印时间戳。 But there can be a case, when B4 AND B8 get the value "Done" the same time, eg because it depends on some other cells in the sheet. 但是可能会有一种情况,例如B4和B8同时获得值“完成”,例如,因为它取决于工作表中的某些其他单元格。 Then the timestamp is printed in K4 and K8 the same time. 然后将时间戳同时打印在K4和K8中。

I got the following function and it's working fine: 我得到了以下功能,并且工作正常:

function onEdit() {

var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("plan");
  var state = ss.getRange("B3:B").getValues();
  var time = new Date();

  for (var row in state) {
    var rowNum = parseInt(row) +1;

    if (hasStatus(state[row] == "Done")) {
                 ss.getRange(rowNum + 2, 11).setValue(time);
        }

    else if ((hasStatus(state[row] == "open") || hasStatus(state[row] == "nothing to do") || hasStatus(state[row] == "locked")))
    {
      ss.getRange(rowNum + 2, 11).clearContent();
      //Browser.msgBox(state);
  }

}
}
function hasStatus(state){
  return state;
}

So, if you set B4 back to open or locked, it erases the content in column K4. 因此,如果将B4设置回打开或锁定状态,它将擦除K4列中的内容。 But it's updating the timestamp in K3:K too on every edit (because it iterates through the whole column everytime). 但是它也会在每次编辑时更新K3:K中的时间戳(因为每次都会遍历整个列)。 How can I prevent this function to update the timestamps in K3:K, if only one row is edited? 如果只编辑一行,如何防止此功能更新K3:K中的时间戳?

Eg B4 and B8 are udpated the same time with "done" -> timestamp in K4 and K8. 例如,B4和B8在K4和K8中用“ done”->时间戳同时补全。 Now you set B5 to "locked" -> timestamps in K4 and K8 are not changed/ updated. 现在,将B5设置为“锁定”-> K4和K8中的时间戳不会更改/更新。

I don't know, if it's just simple or if there's something I don't understand. 我不知道,这是否简单,还是我不了解。 It's hard, because the function has to check every row for it's values, since there can be multiple rows in B which can be updated the same time. 这很困难,因为该函数必须检查每一行的值,因为B中可以有多行可以同时更新。 Can anyone help? 有人可以帮忙吗?

Here's updated onEdit function that checks only for changed rows: 这是更新的onEdit函数,该函数仅检查更改的行:

function onEdit(e) {

  var time = new Date();

  // column B
  var statusColNum = 2;
  // column K
  var timestampColNum = 11;

  // get changed range data
  var range = e.range;
  var rangeCol = range.getColumn();
  var rangeRow = range.getRow();
  var rangeWidth = range.getWidth();
  var rangeHeight = range.getHeight();

  // check if needed sheet was edited
  var curSheet = range.getSheet();
  var curSheetName = curSheet.getName();
  if (curSheetName != 'plan') {
    return;
  }
  // check that current changes are in column B
  if (statusColNum < rangeCol || statusColNum > rangeCol + rangeWidth - 1) {
    return;
  }

  // loop changed rows
  for (var i = 0; i < rangeHeight; i++) {
    var statusCell = curSheet.getRange(rangeRow+i, statusColNum);
    var statusCellVal = statusCell.getDisplayValue();
    var timestampCell = curSheet.getRange(rangeRow+i, timestampColNum);
    var timestampCellVal = timestampCell.getDisplayValue();

    if (statusCellVal == "Done") {
      // do not update cell if already have needed value
      if (timestampCellVal != time) {
        timestampCell.setValue(time);
      }
    }
    else if (["open", "nothing to do", "locked"].indexOf(statusCellVal) !== -1) {
      // do not update cell if already have needed value
      if (timestampCellVal !== '') {
        timestampCell.clearContent();
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM