简体   繁体   中英

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. 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.

So: In B3 appears "Done" and the timestamp is printed in 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. Then the timestamp is printed in K4 and K8 the same time.

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. But it's updating the timestamp in K3:K too on every edit (because it iterates through the whole column everytime). How can I prevent this function to update the timestamps in K3:K, if only one row is edited?

Eg B4 and B8 are udpated the same time with "done" -> timestamp in K4 and K8. Now you set B5 to "locked" -> timestamps in K4 and K8 are not changed/ updated.

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. Can anyone help?

Here's updated onEdit function that checks only for changed rows:

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();
      }
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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