Reputation: 11
At first i want to say sorry for my bad english... Since 7 days i try to program with google script. What i want: After 5 min (for example) thecolums/rows A1:B4 (for example) should be erased. I want to solve that with der sleep function, but it doesen't works. i also doesen't work with a thread. Mybe some of this forum can help me :-)
function test(){
var ss = SpreadsheetApp.getActiveSheet();
var i=0;
for(i; i<10; i++){
if (i=10){
ss.getRange(3,4,9,1).setValue("");
}
else{
Utilities.sleep(5000);
}
}
}
Upvotes: 1
Views: 543
Reputation: 46822
2 things to note :
the EQUAL condition in JavaScript is ==
and not a single =
, so you condition is not working.
Glenn Randers-Pehrson is right in his comment, the condition i==10 will never be true since you count from 0 to 9.
if you rewrite this code correctly it will work as expected.
see code below :
function test(){
var ss = SpreadsheetApp.getActiveSheet();
for(var i=0; i<10; i++){
if (i==9){
ss.getRange(3,4,9,1).setValue("");
}
else{
Utilities.sleep(5000);// argument is in millisecs > total time here is 45 seconds (9*5)
}
}
}
This code can also be written more simply :
function test(){
var ss = SpreadsheetApp.getActiveSheet();
for(var i=0; i<10; i++){
Utilities.sleep(5000);
}
ss.getRange(3,4,9,1).setValue("");
}
Note that Sandy's solution in the other answer is also a possible way to do it...
Upvotes: 0
Reputation: 31320
Have you tried a time-based trigger?
In the Apps Script code editor, use the Resources menu
Unless you only want that code to run once after five minutes?
You can manage a trigger programmatically:
So, you could set a trigger in code to run at a specific time.
Trigger Builder - Run after a certain amount of time
// Creates a trigger that will run 10 minutes later
ScriptApp.newTrigger("myFunction")
.timeBased()
.after(10 * 60 * 1000)
.create();
Upvotes: 1