title: speed up copyto another Google sheets
body: <p>I have a code which copies row if condition.
  By the source sheet is around 8 000 lines and the script fails because is too long.</p>
  <p>How can I speed it up?</p>
  <p>I would like it takes one 1 hr max.</p>
  <p>I think it makes too many calls to Google. I hope we can reduce them.</p>
  <pre><code>function  dispacthtosheets()  { 
  var  sSheet  =  SpreadsheetApp.openById('xxxxxx');
  
  var  srcSheet  =  sSheet.getSheetByName ( &quot;Dispatch&quot; );
  var  lastRow  =  srcSheet.getLastRow ();***emphasized text***
  for  ( var  i  =  1 ;  i  &lt;=  lastRow ;  i ++ )  {
  var  cellule  =  srcSheet.getRange ( &quot;L&quot;  +  i );
  var  val  =  cellule.getValue();
  if  ( val  ==  &quot;Changement ENT&quot; )  {
    var  tarSheet3  =  sSheet.getSheetByName ( &quot;Changement ENT&quot; );
    var  srcRange  =  srcSheet.getRange ( &quot;C&quot;  +  i  +  &quot;:R&quot; +  i  );
    var  tarRow3   =  tarSheet3.getLastRow (); 
    var  tarRange3 =  tarSheet3.getRange ( &quot;C&quot;  +  tarRow3  +  &quot;:R&quot;  +  tarRow3);
    srcRange.copyTo (tarRange3);
    tarSheet3.insertRowAfter ( tarRow3 ); 
    }    if ( val  ==  &quot;VC en REL&quot; )  {
        var  tarSheet1  =  sSheet.getSheetByName ( &quot;VC en REL&quot; );
        var  srcRange  =  srcSheet.getRange ( &quot;C&quot;  +  i  +  &quot;:R&quot; +  i  );
        var  tarRow1  =  tarSheet1.getLastRow (); 
        var  tarRange1  =  tarSheet1.getRange ( &quot;C&quot;  + tarRow1  +  &quot;:R&quot;  + tarRow1);
        srcRange.copyTo (tarRange1);
        tarSheet1.insertRowAfter ( tarRow1 );
    }    if  ( val  ==  &quot;Solo à étudier&quot; )  {
            var  tarSheet2  =  sSheet.getSheetByName ( &quot;Solo à étudier&quot; );
            var  srcRange  =  srcSheet.getRange ( &quot;C&quot;  +  i  +  &quot;:R&quot; +  i  );
            var  tarRow2   =  tarSheet2.getLastRow (); 
            var  tarRange2 =  tarSheet2.getRange ( &quot;C&quot;  +  tarRow2 +  &quot;:R&quot;  +  tarRow2);
            srcRange.copyTo (tarRange2);
            tarSheet2.insertRowAfter ( tarRow2 );
    }  if ( val  ==  &quot;VC + 6 mois&quot; )  {
              var  tarSheet4  =  sSheet.getSheetByName ( &quot;VC + 6 mois&quot; );
              var  srcRange  =  srcSheet.getRange ( &quot;C&quot;  +  i  +  &quot;:R&quot; +  i  );
              var  tarRow4  =  tarSheet4.getLastRow (); 
              var  tarRange4  =  tarSheet4.getRange ( &quot;C&quot;  + tarRow4  +  &quot;:R&quot;  + tarRow4);
              srcRange.copyTo (tarRange4);
              tarSheet4.insertRowAfter ( tarRow4);
    }    if  ( val  ==  &quot;Sortie Nat&quot; )  {
                  var  tarSheet5  =  sSheet.getSheetByName ( &quot;Sortie Nat&quot; );
                  var  srcRange  =  srcSheet.getRange ( &quot;C&quot;  +  i  +  &quot;:R&quot; +  i  );
                  var  tarRow5  =  tarSheet5.getLastRow (); 
                  var  tarRange5  =  tarSheet5.getRange ( &quot;C&quot;  + tarRow5  +  &quot;:R&quot;  + tarRow5);
                  srcRange.copyTo (tarRange5);   
                  tarSheet5.insertRowAfter ( tarRow5 );
    }    if  ( val  ==  &quot;ZCTM&quot; )  {
                      var  tarSheet6  =  sSheet.getSheetByName ( &quot;ZCTM&quot; );
                      var  srcRange  =  srcSheet.getRange ( &quot;C&quot;  +  i  +  &quot;:R&quot; +  i  );
                      var  tarRow6  =  tarSheet6.getLastRow (); 
                      var  tarRange6  =  tarSheet6.getRange ( &quot;C&quot;  + tarRow6  +  &quot;:R&quot;  + tarRow6);  
                      srcRange.copyTo (tarRange6);
                      tarSheet6.insertRowAfter ( tarRow6 );
    }   if  ( val  ==  &quot;Appro validée&quot; )  {
                          var  tarSheet7  =  sSheet.getSheetByName ( &quot;Appro validée&quot; );
                          var  srcRange  =  srcSheet.getRange ( &quot;C&quot;  +  i  +  &quot;:R&quot; +  i  );;
                          var  tarRow7  =  tarSheet7.getLastRow (); 
                          var  tarRange7  =  tarSheet7.getRange ( &quot;C&quot;  + tarRow7  +  &quot;:R&quot;  + tarRow7);
                          srcRange.copyTo (tarRange7);
                          tarSheet7.insertRowAfter ( tarRow7 );
    }   if  ( val  ==  &quot;F943&quot; )  {
                          var  tarSheet8  =  sSheet.getSheetByName ( &quot;F943&quot; );
                          var  srcRange  =  srcSheet.getRange ( &quot;C&quot;  +  i  +  &quot;:R&quot; +  i  );
                          var  tarRow8  =  tarSheet8.getLastRow (); 
                          var  tarRange8  =  tarSheet8.getRange ( &quot;C&quot;  + tarRow8  +  &quot;:R&quot;  + 
             tarRow8);
                          srcRange.copyTo (tarRange8);
                          tarSheet8.insertRowAfter ( tarRow8 );
                          }                                                                            
     sSheet.toast(' VC dispacthé !', 'Confirmation ');
  }
  };
  </code></pre>