title: Google App Script - Google Spreadsheets Move Row based on cell value efficiently
body: <p>I am trying to move rows based on cell values, the function I have written works.  However when there are more than 24 rows to move the script times out.  Is there any way to make the loop run faster or more efficiently, as it will always time out.  </p>
  
  <p>The script reads the cell value of column 7(G) and if it matches one of the sales persons names it copies it over to the sheet that has their name.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function CopyDataToNewFile() {
      
    // How Many Columns over to copy
    var columsCopyCount  = 11; // A=1 B=2 C=3 ....
    
    // What Column to Monitor
    var columnsToMonitor  = 7; // A=1 B=2 C=3 ....MONITORS SALES PERSON NAME
    
     //TARGET SPREAD SHEETS
    var salesPerson1  = "Lorna";
    var salesPerson2  = "Sarah";
    var salesPerson3  = "Mark";
   
    //SOURCE SPREAD SHEET
    var ss = SpreadsheetApp.openById('1S3F0Dekyda4g77j_a150Obz0IDNKtWMU2WlGDSXdcD4');
    var sourceSpreadSheetSheetID = ss.getSheetByName("importdata");
    var sourceSpreadSheetSheetID1 = ss.getSheetByName(salesPerson1);
    var sourceSpreadSheetSheetID2 = ss.getSheetByName(salesPerson2);
    var sourceSpreadSheetSheetID3 = ss.getSheetByName(salesPerson3);
    
    var numRows = sourceSpreadSheetSheetID.getLastRow()+1;
      
    
    for(var i = 2; i &lt; numRows; i++)
    {
    var r = sourceSpreadSheetSheetID.getRange(i, columnsToMonitor);
  
    var rValue = r.getValue();
      
    if(rValue == salesPerson1) {
        var targetRange = sourceSpreadSheetSheetID1.getRange(sourceSpreadSheetSheetID1.getLastRow() + 1, 1);
        sourceSpreadSheetSheetID.getRange(i, 1, 1, columsCopyCount).copyTo(targetRange), {contentsOnly: true};
        //sourceSpreadSheetSheetID.deleteRow(i);
      
      }else if (rValue == salesPerson2) {
          var targetRange = sourceSpreadSheetSheetID2.getRange(sourceSpreadSheetSheetID2.getLastRow() + 1, 1);
        sourceSpreadSheetSheetID.getRange(i, 1, 1, columsCopyCount).copyTo(targetRange), {contentsOnly: true};
        //sourceSpreadSheetSheetID.deleteRow(i);
        
      }else if (rValue == salesPerson3) {
         var targetRange = sourceSpreadSheetSheetID3.getRange(sourceSpreadSheetSheetID3.getLastRow() + 1, 1);
        sourceSpreadSheetSheetID.getRange(i, 1, 1, columsCopyCount).copyTo(targetRange), {contentsOnly: true};
        //sourceSpreadSheetSheetID.deleteRow(i);
      
      }else {//Fail Safe
        var targetRange = sourceSpreadSheetSheetID1.getRange(sourceSpreadSheetSheetID1.getLastRow() + 1, 1);
        sourceSpreadSheetSheetID.getRange(i, 1, 1, columsCopyCount).copyTo(targetRange), {contentsOnly: true};
        //sourceSpreadSheetSheetID.deleteRow(i);
      }
    
    }
    
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>Here is a copy of the sheet with the script.  The script can be run from the script editor.</p>
  
  <p><a href="https://docs.google.com/spreadsheets/d/1S3F0Dekyda4g77j_a150Obz0IDNKtWMU2WlGDSXdcD4/edit?usp=sharing" rel="nofollow noreferrer">Google Sheet Link</a></p>
  
  <p>Thank you</p>