1

I am trying to create stopwatch functionality in google spreadsheet using google apps script. I have a cell which displays the counter and should be incremented after every millisecond when I press the start button.and it should stop whenever I press the stop button.

Currently my problem is that my spreadsheet is not updated after every second/millisecond during the script execution, the cell in the spreadsheet only updates after the script has finished executing.

can any one help or is this feature not possible?

var ss= SpreadsheetApp.getActiveSpreadsheet();
var ss1=ss.getSheetByName("stopwatch"); 


function timerstart() {  

  for(i=0;i<10000;i++){

   Utilities.sleep(10);
   ss.getRange('B6').setValue(i);

 }
}
Ibrahim Anjum
  • 11
  • 1
  • 5
  • adding `SpreadsheetApp.flush();` will force the update but the timer still won't function smoothly. This type of script is not recommended (making 10000 writes), you would be better off using the HTML service and have the timer run client side instead. – James D Jul 19 '18 at 11:05
  • 1
    Mixed in with your stopwatch is remote URL requesting, server data synchronization, and client UI redrawing. This means your 10ms resolution is meaningless. Further, each new script execution acts independently of previous ones, so you cannot use Apps Script to stop a running Apps Script function. Use a sidebar to eliminate the remote data issues. Read in-depth about Apps Script and client-server communication methods. I'm quite sure there are lots of examples of HTML stopwatches so do your research. – tehhowch Jul 19 '18 at 11:23
  • 1
    @pnuts even better :D my comment was based on the `Utilities.sleep` call. – tehhowch Jul 19 '18 at 12:14
  • I am going to try this with an HTML service: https://developers.google.com/apps-script/guides/html/ . I didn't knew that it is possible to deploy client side code in google sheets. thanks – Ibrahim Anjum Jul 19 '18 at 13:28
  • Are you willing to post your answer (and answer your own question with it)? I'm looking for the same feature. – Torsten Nov 24 '19 at 12:55
  • Updating the google spreadsheet every second is not feasible even if possible. I utilized the sidebar widget to achieve the desired functionality. https://www.youtube.com/watch?v=QENu30wS3Zw https://developers.google.com/apps-script/guides/dialogs – Ibrahim Anjum Nov 24 '19 at 15:23
  • functionality developed here: https://docs.google.com/spreadsheets/d/1ybHf7XsymqHD3OH3LaNhaYiMFqxCl_5PCSI0BW5Z8X8/edit?usp=sharing – Ibrahim Anjum Nov 24 '19 at 15:33

0 Answers0