Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

113
Views
cascading row deletion javascript

I start in the programming and in the google sheet folder I would like to delete the lines after 60 days according to the date column L (12). the program works but as soon as a line is deleted, all the other lines are deleted. find below my code on google app script.

//trouver le nom du classeur
  var date1 ; 
  var date2 = new Date();
  var classeur = SpreadsheetApp.getActiveSpreadsheet();
  var couleur ;
  var feuilles = classeur.getSheets();
  // for (var i=0 ; i<sheets.length ; i++) out.push( [ sheets[i].getName() ] )

//trouver le nombre de feuilles dans le classeur ???


 
//pour chaque feuille, il faut
//pour chaque ligne, étudier la cellule L
//si vide ou < 31 alors rien
//si >= effacer la ligne 
  
function coloriage() {
  for (var i=0 ;i<feuilles.length ; i ++) {
    var feuille = feuilles[i] ;
    var transports = feuille.getDataRange();
    var donnees = transports.getValues();    
    for (var ligne = 6 ; ligne < donnees.length ; ligne++) {        
      date1 = donnees[ligne][11]; 
      if (date1 != "") {
        // différence de jours
        var jours = Math.round((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
        //Logger.log("ligne= "+ligne+ " date1= "+date1+" jours= "+jours);
        if (jours < 40) {
          if (jours<20) { couleur = "#fff" ; }
          if ((jours>=20)&&(jours<30)) { couleur = "#fd0" ; }
          if ((jours>=30)&&(jours<40)) { couleur = "#f75" ; }
          feuille.getRange(ligne+1,1,1,12).setBackground(couleur);
          }
        else { 
        feuille.deleteRows(ligne+1,1)
        Logger.log("ligne "+(ligne+1)+" supprimée ; nb de jours "+jours);
        ligne --
        }
      }
    }
  }
}

Thank you in advance for your help.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You have a loop with line, and inside the loop you affect the value of line ligne -- ... weird. If you want to remove rows, reverse your loop as follows

for (var ligne = donnees.length-1 ; ligne >= 6 ; ligne--)
about 4 years ago · Juan Pablo Isaza Report

0

You can also simply remove the ligne -- instruction as whenever the code ends up on the else branch, it creates an infinite loop and all the rows end up being deleted.

Therefore, your else will look simply like this:

else { 
   feuille.deleteRows(ligne+1,1);
   Logger.log("ligne "+(ligne+1)+" supprimée ; nb de jours "+jours);
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!