简体   繁体   中英

Time stamps in Google scripts

Getting difference between time stamps

playing with code

function checkCompleted() {
  var s = SpreadsheetApp.getActive().getSheetByName('Timestamps');
  s.getRange('D2:E').getValues().forEach (function (r, i) {
    if(r[0] == '2' && !r[1]){
      s.getRange(i + 2, 5).setValue(new Date())
    } else if (r[0] == '1'){
      s.getRange(i + 2, 5).clearContent()
    }
  }
)}

For context a checkbox is used for Column D if checked its true if unchecked its false if that column - = 2 get the range and set a date and if it = 1 clear the date this is used as a way to see time differences between when the boxes are checked and unchecked.

My question is although I found a formula that works I do not understand the i+2 I understand column 5 is where the timestamp is and but am unclear on the i+2 part

i+2

is offset for row because you start from D2:E

so:

s.getRange(i + 2, 5).setValue(new Date())

means that "put/remove date in 5th column offset by 1 row"


0

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