Search code examples
google-sheetscountdown

Make a timer for competitions with buttons


I need help. I need to make a countdown timer, with two buttons (start and stop).

However, this timer needs to change, at one point it needs to be a 1 minute timer, at another time it needs to be a 2 minute timer, 4 minutes timer, 5 minute timer and others.

make a timer for competitions

Make it possible to use it in a Google Spreadsheet


Solution

  • try the following 1st of all - Open your Google Spreadsheet. :)

    now Click on "Extensions" in the menu bar.

    then Select "Apps Script" from the dropdown menu. In the Apps Script editor, paste the below code.

    function startTimer() {
      var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
      var time = sheet.getRange('A1').getValue();
      
      var timer = setInterval(function() {
        time--;
        sheet.getRange('A1').setValue(time);
        
        if (time <= 0) {
          clearInterval(timer);
          sheet.getRange('A1').setValue('Time\'s up!');
        }
      }, 1000);
    }
    
    function stopTimer() {
      var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
      sheet.getRange('A1').setValue('Timer stopped');
    }
    
    1. then save the script by clicking on the floppy disk icon or just pressing Ctrl + S.

    now Close the Apps Script editor.

    Now, you should see two new buttons in your spreadsheet named : "Start Timer" and "Stop Timer".

    3.now To set the countdown time according to your req. , enter the number of minutes in cell A1.

    1. Click on the "Start Timer" button to start the countdown. The timer will decrement the value in cell A1 every second until it reaches zero.

    Once the timer reaches zero, it will display "Time's up!" in cell A1. If you want to stop the timer before it reaches zero, click on the "Stop Timer" button.

    you can change the cell to any according to you comfort instead of "A1" on the other hand you can simply hard code it in code and use if/else the drive logical approach the various req. for deferent time ranges .

    let me know if it does help you or not .