dimanche 27 janvier 2019

Google Sheets Script - One button with two consecutive functions (function 1 runs, ends then function 2 runs)

I want to have two functions inside a funtion rolarD20. The first, rolarDados generates a random number between 1 and 20, and the second, fixar, fixes the number to the cell so that it doesn't get generated constantly, a new random number will only be generated when on the click of the button for rolarD20 again.

However, the way I got it to work makes it necessary to have two buttons with one function each, because when I try to create a "master" function with the two functions working under it, the first it doesn't run:

function rolarD20(){
  rolarDados();
  fixar();
}

function rolarDados() {

  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('S5').setFormula('=RANDBETWEEN(1,20)');

}

function fixar() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('S5').activate();
  spreadsheet.getRange('S5').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);

};

I'm guessing this is because the function is trying to do everything at once. But for the logic to work, it needs to properly run the first one, end it and THEN run the second one.

When I have two buttons, one for rolarDados and another one for fixar, it works. I just need help to simplify this by having just one button click for the two functions in sequence and, if possible, only one value being generated in cell S5. Because as it is with two buttons now, it generates one random number when function rolarDados runs (value which is being discarded by the second button run) and a second random number on the run of fixar, which will be stored in the cell.




Aucun commentaire:

Enregistrer un commentaire