How to call a function within itself? Recursiveness

Asked

Viewed 89 times

1

Goal

Make a recursion within the function SALVARHISTO() so that the function is called again, if the cell A8 is not found.

What I tried to

I tried to put a condition at the end, if the A8 cell was not empty, the function would run until I found the A8 cell empty. Someone can help me?

Code

function SALVARHISTO() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('A7:L7').activate();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Histórico'), true);
  spreadsheet.getRange('7:7').activate();
  spreadsheet.getActiveSheet().insertRowsBefore(spreadsheet.getActiveRange().getRow(), 1);
  spreadsheet.getActiveRange().offset(0, 0, 1, spreadsheet.getActiveRange().getNumColumns()).activate();
  spreadsheet.getRange('A7').activate();
  spreadsheet.getRange('\'Produção mensal\'!A7:L7').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
  spreadsheet.getActiveRangeList().setBackground('#dbe5f1')
  .setBorder(true, true, true, true, true, true, '#000000', SpreadsheetApp.BorderStyle.SOLID)
  .setBorder(false, false, false, false, false, false)
  .setBorder(null, null, true, null, null, null, '#000000', SpreadsheetApp.BorderStyle.SOLID)
  .setBorder(null, null, null, true, null, null, '#000000', SpreadsheetApp.BorderStyle.SOLID);
  spreadsheet.getRange('A7').activate();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Produção mensal'), true);
  spreadsheet.getRange('A6').activate();
  
  var x = sheet.getRange('A8');
  if( x != " "){
    function SALVARHISTO(){}
  
  }else{
  return 'Salvo com sucesso.';
  }
  
};
  • Hello Camilla, if you put 3 bass accents (```) before and after your code, it will stay monoespaçado and easier to read here at Stackoverflow.

  • I don’t think trying to solve this problem is such a good idea. You will be running this function SALVARHISTO many times per second (maybe tens, hundreds or thousands). Put a setTimeOut of 1 second would be something better, but perhaps it is possible to make use of listeners for that reason.

  • That function SALVARHISTO(){} declares a function and does not execute the function

  • 1

    For you to perform a function with recursiveness just call it again within the function. SALVARHISTO(). Speechless Function in front

  • Hello @Brunocunha, I believe that really is the problem, will q you can add as answer?

2 answers

0

Hello!

For you to perform a function with recursiveness just call it again inside the SAVE() function without the word Function in front, because Function serves for you to declare a function.

function SALVARHISTO() {
  // some code
  if( x != " "){
    SALVARHISTO();  
  }
}

0

For you to perform a function with recursiveness just call it again inside the function SAVE HISTO() without the word Function in front, because Function serves for you to declare a function.

function SALVARHISTO() {
 // some code
 if( x != " "){
   SALVARHISTO();  
 }
}

Bruno already gave the answer there, whenever you just go execute its function, you must invoke it using the format nomeDaFuncao(parametro), without the reserved word function in front.

But I would say beware of recursiveness given the simplicity of your if. The chance of a mistake like stack overflow (exceed the call limit of a function/method) if you are not completely sure of possible returns from .getRange() can be dangerous.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.