Making a script that runs every second
I am trying to create a script that edits a specific cell on a specific sheet every second with random text like "SetTime".
This particular cell: X2
This private sheet: "System_Info"
You may ask why I need this, essentially I have a cell that displays the time using the = NOW formula. When the spreadsheet is edited, it will update the = NOW formula.
So, I need a script that loops through every second and runs a function that edits that cell.
I used this:
setInterval(function(){ SpreadsheetApp.getSheet("System_Info").getRange('X2').setValue('SetTime'); }, 1000);
However, the target interval is undefined.
Thanks for any help,
Shaun.
You are mixing server with client code. even if you are using a time-driven application, the scripts cannot run it because they run no more than once a minute and changes through the API do not trigger an update.
Alternative: go to spreadsheet menu, file, properties. Select the option to update the calculated functions every minute. No script required.
Here is a function that will update the cell time every second for 15 seconds. This should at least be 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();
}
}