Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a script that runs every second

I am trying to create a script that edits a particular cell on a particular sheet every second with a random text like, "SetTime".

This Particular Cell is: X2

This Particular Sheet is: "System_Info"

You may ask why I need this, essentially, I have a cell that displays a time using the =NOW formula. When a spreadsheet is edited, it will refresh the =NOW formula.

So, I need a script that loops every second and runs a function that edits that cell.

I've used this:

setInterval(function(){ SpreadsheetApp.getSheet("System_Info").getRange('X2').setValue('SetTime'); }, 1000);

However, set interval is not defined.

Thanks for any help,

Shaun.

like image 488
Shaun Cockram Avatar asked Nov 17 '15 17:11

Shaun Cockram


2 Answers

you are mixing server with client code. even if you use time driven apps script triggers its not possible because they run at most once a minute, and changes through api do not cause a refresh.

Alternative: go to spreadsheet menu,file,properties. Select the option to update calculated functions every minute. No script needed.

like image 175
Zig Mandel Avatar answered Nov 15 '22 00:11

Zig Mandel


Here is a function that will update the time in a cell every second for 15 seconds. It should be at least a starting point for you.

function updateCell() {
  for (i=0; i<15; i++){
    Utilities.sleep(1000);
    var date = new Date();
    SpreadsheetApp.getActiveSheet().getRange("A1").setValue(date);
    SpreadsheetApp.flush();
  } 
}
like image 41
Bjorn Behrendt Avatar answered Nov 15 '22 01:11

Bjorn Behrendt