Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

javascript - Why does my Google App script auto refresh code not work?

I wrote a simple code to automatically refresh the digits in the Google sheet. I print the i with app script to the relevant place. Data needs to be refreshed every 15 seconds. But it doesn't work right. It should write 0 at the 15th second, 1 at the 30th, 2 at the 45th. But 0 doesn't write 1, it just writes the 2 in the last loop. Why doesn't it write data every 15 seconds?

function exampleFunction() {
  var app = SpreadsheetApp;
  var spreadSheet = app.getActiveSpreadsheet();
  var activeSheet = spreadSheet.getSheetByName("sheetname");
  var i = 0;

  while(true){
    activeSheet.getRange(1, 1).setValue(i);
    i++;
    Utilities.sleep(15000);
    
    if(i == 3){
      break;
    }
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For documentation reasons, I can confirm Tanaike's comment, it works with SpreadsheetApp.flush() as it is expected:

function exampleFunction() {
  var app = SpreadsheetApp;
  var spreadSheet = app.getActiveSpreadsheet();
  var activeSheet = spreadSheet.getSheetByName("sheetname");
  var i = 0;

  while(true){
    activeSheet.getRange(1, 1).setValue(i);
    SpreadsheetApp.flush()
    i++;
    Utilities.sleep(15000);
    
    if(i == 3){
      break;
    }
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...