1

I notice that with the addition of several SpreadsheetApp.flush();, the time it takes the script to finish is absurdly longer, I understand because of the fact that it needs to update the spreadsheet at each end of the functions.

But I need a confirmation of the 100% updated spreadsheet to pass the next function, is the way I'm using it correct or is there a more correct way to work this functionality?

This amount of calls and flushes can cause any problems in walking the script?

function AutomacaoCompleta() {
  if (SpreadsheetApp.getActive().getSheetByName('Dados Importados').getRange("A2").getValues()[0][0]=="OK"){
    LimparDados();
    SpreadsheetApp.flush();
    
    PaginaDoJogo();
    SpreadsheetApp.flush();
    
    EstadioDoJogo();
    SpreadsheetApp.flush();
    
    MenuDoPlantelTimeA();
    SpreadsheetApp.flush();
    
    PlanteldoTimeA();
    SpreadsheetApp.flush();
    
    MenuDoPlantelTimeB();
    SpreadsheetApp.flush();
    
    PlanteldoTimeB();
    SpreadsheetApp.flush();
    
    EstatisticasDoTimeA();
    SpreadsheetApp.flush();
    
    EstatisticasDoTimeB();
    SpreadsheetApp.flush();
    
    MenuDoPlantelTimeA1();
    SpreadsheetApp.flush();
    
    MenuDoPlantelTimeB1();
    SpreadsheetApp.flush();
  }
}

Digital Farmer
  • 1,705
  • 5
  • 17
  • 67
  • 1
    I would try to combine that if I had the function definitions. – Cooper Aug 13 '21 at 19:15
  • Hi @Cooper . Each of the functions are huge, Unfortunately I tried to add in question but it advanced to the character limit. But I can say that they are functions that import data from other spreadsheets and websites and send the values to the project spreadsheet pages. So I decided to ask a more general question about usage. – Digital Farmer Aug 13 '21 at 19:19
  • 1
    Yes but are they huge because it's a complicated problem or are they huge because you don't know what you're doing? My guess is the latter. – Cooper Aug 13 '21 at 19:51

1 Answers1

1

Unless you provide the exact code (as it is possible that it can still be optimized), what I can say about flush is that it should be fine to have multiple calls except that it can possibly increase your runtime compared to less or no flush at all.

Basically, it makes sure the previous pending commands are all executed before proceeding. Without flush, it tries to optimize your operations by executing them by batch.

These optimizations have benefits and drawbacks.

Main benefit is that it would improve the performance of the script as it reduces your runtime.

Main drawback is that, it might not execute your commands the way you want it to be. (I'm not really sure how it is being optimized so anything can happen here)

So aside from possible increase in runtime, you should be fine doing multiple flush calls.

Reference:

NightEye
  • 10,634
  • 2
  • 5
  • 24
  • 1
    Thanks for the answer @NaziA , I know there's no way to be detailed even due to the lack of codes for my functions, but really my biggest question was whether it could cause some kind of looping in the updates that could give some problem. With your answer my doubt was resolved! – Digital Farmer Aug 13 '21 at 22:00