title: How to copy a range of data to another google sheet using google apps script?
body: <p><strong>Source:</strong>
  <a href="https://i.stack.imgur.com/OElZQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OElZQ.png" alt="enter image description here" /></a></p>
  <p><strong>Destination:</strong>
  <a href="https://i.stack.imgur.com/n6EFE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/n6EFE.png" alt="enter image description here" /></a></p>
  <p>Hi everyone,</p>
  <p>I have 2 google sheets, <code>Source</code> &amp; <code>Destination</code>. I want to copy a range of data from Source sheet to Destination sheet by using google apps script. So the data should appear at the last row of the Destination sheet. This is my code:</p>
  <pre><code>function copyInfo() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var copySheet = ss.getSheetByName(&quot;Source&quot;);
    var pasteSheet = ss.getSheetByName(&quot;Destination&quot;);
  
    // get source range
    var source = copySheet.getRange(3,1,1,10);
    // get destination range
    var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,1,10);
  
    // copy values to destination range
    source.copyTo(destination);
  
  }
  </code></pre>
  <p>Seems like the script only allow me to do the copying within one google sheet (different tab) instead of 2 different google sheet. May I know how should I modified it so that the script able to find my destination google sheet and paste the data at the last row? Any help will be greatly appreciated!</p>
  <p><strong>Edit</strong>
  <a href="https://i.stack.imgur.com/uAbbd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uAbbd.png" alt="enter image description here" /></a></p>
  <p>I tried to use <code>onEdit</code> to triggered the script, however it seems like not working. Below is my script:</p>
  <pre><code>function onEdit(e) {
  
    if (e.range.columnStart == 11 &amp;&amp; e.range.rowStart == 3){
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      const id ='1r5Hygl5ysahMi6DQ3duDR5L8c4aGX_0CJba7lXxnejw';
      var copySheet = ss.getSheetByName(&quot;Sheet1&quot;);
      var pasteSheet = SpreadsheetApp.openById(id).getSheetByName(&quot;Sheet1&quot;);
      
      if (e.value == 'Submit'){
        var source = copySheet.getRange(3,1,1,10);
        const values = source.getValues();
        var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,1,10);
        destination.setValues(values);
      }
    }
  }
  </code></pre>
  <p>Source:
  <a href="https://docs.google.com/spreadsheets/d/1jnOvE-Dc7y9o7GsgN9fjOXZWlUCwiJayugX5q4Y3CA0/edit#gid=0" rel="nofollow noreferrer">https://docs.google.com/spreadsheets/d/1jnOvE-Dc7y9o7GsgN9fjOXZWlUCwiJayugX5q4Y3CA0/edit#gid=0</a></p>
  <p>Destination:
  <a href="https://docs.google.com/spreadsheets/d/1r5Hygl5ysahMi6DQ3duDR5L8c4aGX_0CJba7lXxnejw/edit#gid=0" rel="nofollow noreferrer">https://docs.google.com/spreadsheets/d/1r5Hygl5ysahMi6DQ3duDR5L8c4aGX_0CJba7lXxnejw/edit#gid=0</a></p>