Click here to Skip to main content
15,886,075 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm creating a simple time tracker for time and motion study purposes in Google sheets. The current code that I'm using only records start time and end time as shown on the screenshot below.

Here's the Google Sheet tracker:
Tracker - Google Sheets[^]

What I would like to do is for it to have the ability to pause the timer and then have the ability to resume anytime. Does anyone know how to do this? Can the script of pause and resume be assigned to another dropdown option from the data validation in column h, say "pause" and resume" options?

What I have tried:

Below is the current code I'm using, credits to Cooper who helped me over in stackoverflow website.

function onEdit(e) {
  const sh = e.range.getSheet();
  if (sh.getName() == "Sheet1" && e.range.columnStart == 2 && e.value) {
    e.range.offset(0, 7).setValue(new Date())
  }
  if (sh.getName() == "Sheet1" && e.range.columnStart == 8 && e.value) {
    e.range.offset(0, 2).setValue(new Date());
    let Start = new Date(sh.getRange(e.range.rowStart,8).getValue());
    let End = new Date(sh.getRange(e.range.rowStart,9).getValue());
    sh.getRange(e.range.rowStart,10).setValue(timeDiff1(Start,End));
  }

}

function timeDiff1(Start, End) {
  if (Start && End) {
    var second = 1000;
    var minute = 60 * second;
    var t1 = new Date(Start).valueOf();
    var t2 = new Date(End).valueOf();
    var d = t2 - t1;
    var minutes = Math.floor(d % day % hour / minute);
    var seconds = Math.floor(d % day % hour % minute / second);
    return 'mm:ss\n' + minutes + ':' + seconds;
  }
  else {
    return 'Invalid Inputs';
  }
}
Posted
Updated 14-Sep-22 4:14am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900