title: Google sheet app script - Copy data from one sheet to another based on condition with loop
body: <p>I am new to the forum and new to Google App Script.
  I have a sheet with multiple tabs.
  I want to do is probably very simple, but seems quite complex to me, coming from VBA...</p>
  <p>I would like to: IF Facture(lastrow, 5) = 1 THEN copy-paste DetailDevis(i,1) to DetailFacture(lastrow,3) every time DetailDevis(i,2) = Facture(lastrow,2)</p>
  <p>I have made the following, and i would love to get feed back as of why it does not work...</p>
  <pre><code>    function onEdit()
  {
   var ss = SpreadsheetApp.getActiveSheet()
   var Facture = ss.getSheetByName(&quot;FactureDevis&quot;) ;
   var FactureDetail = ss.getSheetByName(&quot;DetailFactureDevis&quot;) ;
   var DevisDetail = ss.getSheetByName(&quot;DetailDevis&quot;) ;
   var FactureLR = Facture.getLastrow() ;
   var Situ1R = Facture.getRange(FactureLR,5) ;
   var Situ1V = Situ1R.getValues() ;
   var FactureDevisR = Facture.getRange(FactureLR,2) ;
   var FactureDevisV = FactureDevisR.getValues() ;
   var DevisDetailLR = DevisDetail.getLastrow() ;
   var FactureDetailLR = FactureDetail.getLastrow() ;
  if (Situ1V == 1)
  {
      for (var i = 2 ; i &lt;= DevisDetalLR ; i++) {
   if (DevisDetail.getRange(i,2).getValue() == FactureDevisV)
   {
  DevisDetail.getRange(i,1).copyTo(FactureDetail.getRange(FactureDetailLR,3);
  }}}
  </code></pre>
  <p>Here are some additional questions:</p>
  <ul>
  <li>Is getLastrow() dynamic?</li>
  <li>Is the place you set your var crucial? Or can you set them all at the beginning?</li>
  </ul>