Google Sheets Stopwatch With a Start and Stop Button

I’m a total beginner in Google Apps Script and Java but I need a stopwatch in Google Sheets with a start and stop function. Ideally the button should be a toggle button so I can start and stop the stopwatch with only one single button. And the time should be displayed. Could someone help me out? Thanks in advance 🙂

Answer

Not quite what you want but here’s a quick 2 button method:

function startTimer() {
  stopwatch('start');
}
function stopTimer() {
  stopwatch('stop');
}
function stopwatch(mode) {
  if (mode == 'start') {
    var start = new Date().getTime();
    SpreadsheetApp.getActiveSheet().getRange('D8').setValue(start);
    SpreadsheetApp.getActiveSheet().getRange('D7').setValue('Running...');
    SpreadsheetApp.getActive().toast('Stopwatch running...');
  }

  if (mode == 'stop') {
    var start =  SpreadsheetApp.getActiveSheet().getRange('D8').getValue();
    if (!start) {
      SpreadsheetApp.getActiveSheet().getRange('D7').setValue('Not Started');
    }
    var end = new Date().getTime();
    var elapsed = (end - start) / 1000;
    SpreadsheetApp.getActiveSheet().getRange('D7').setValue(elapsed);
    SpreadsheetApp.getActive().toast('Stopwatch off.');
  }
}

enter image description here

Google google sheets create button to find out how to create the 2 buttons, then assign each to the start and stop functions.

If you really want one button, read up on how to modify the button (drawing) methods between function calls here: Ref – Drawing

Source: stackoverflow
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .