title: Google Script: How to CopyRows from one sheet to another spreadsheet
body: <p>I'm trying to copy rows from one sheet to another spreadhsheet using google sciprt but the same is not working.</p>
  
  <p>Please could you help me or explain me how to do this works?</p>
  
  <pre><code>   function CopyRows() {
      var source = SpreadsheetApp.openById('XXXXXX');
      var sourcesheet = source.getSheetByName('sheet1');
  
      var target = SpreadsheetApp.openById('YYYYY')
      var targetsheet = target.getSheetByName('sheet1');
      var targetrange = targetsheet.getRange(targetsheet.getLastRow() + 1 , 1);
      sourcesheet.getRange(2, 1, sourcesheet.getLastRow(), sourcesheet.getLastColumn()).copyTo(targetrange);
  
     }
  </code></pre>
  
  <p>Do you know if exist anyway to copy rows from one sheet to another sheet in other spreadsheet?</p>
  
  <p>Best Regards</p>
  
  <hr>
  
  <h1>EDIT: 01/07/2013</h1>
  
  <p>Thanks PhysLabTsar , your script works very well for copyrows from one sheet to other spreadsheet.</p>
  
  <pre><code>   function myFunction() {
       var source = SpreadsheetApp.openById('xxxxxx');
         var sourcesheet = source.getSheetByName('sheet1');
  
         var target = SpreadsheetApp.openById('xxxxx')
         var targetsheet = target.getSheetByName('sheet1');
  var targetrange = targetsheet.getRange(2, 1, sourcesheet.getLastRow(), sourcesheet.getLastColumn());
         var rangeValues = sourcesheet.getRange(2, 1, sourcesheet.getLastRow(), sourcesheet.getLastColumn()).getValues();
         targetrange.setValues(rangeValues);
  </code></pre>
  
  <p>}</p>
  
  <hr>
  
  <p>EDIT: CopyRows from Multiples Sheets</p>
  
  <pre><code>   function ShellCopyValues(){
       var sheets = ['sheet1','sheet2','sheet3','sheet4','sheet4'];
       for (var s in sheets){
     CopyRows(sheets[s]);
       }
     }
  
     function CopyRows(uname) {
       var source = SpreadsheetApp.openById('XXXXXX');
         var sourcesheet = source.getSheetByName(uname);
  
         var target = SpreadsheetApp.openById('YYYYYY')
         var targetsheet = target.getSheetByName(uname);
         var targetrange = targetsheet.getRange(2, 1, sourcesheet.getLastRow(), sourcesheet.getLastColumn());
         var rangeValues = sourcesheet.getRange(2, 1, sourcesheet.getLastRow(), sourcesheet.getLastColumn()).getValues();
         targetrange.setValues(rangeValues);
  
     }
  </code></pre>
  
  <p>If anyone have a best way to do it. Please feelfree to letme know.</p>
  
  <p>Best Regards,</p>