title: Copy data from cells in sheet1 to different non-continuous columns in sheet2 with google apps script
body: <p>In a google spreadsheet document I have two sheets, one called BD Products and another called Add product.</p>
  <p>The first sheet (BD Products) is a table with all the information of the products (by rows). Some cells contain formulas to make calculations automatically. Other cells are empty fields that must be filled in by hand. To do this, I have created a form on the &quot;Add product&quot; sheet where there are those empty fields on the first sheet that are to be filled in manually.</p>
  <p>My intention is to send that data from the form of the &quot;Add product&quot; sheet to the &quot;BD Products&quot; sheet when clicking on the submit button. The problem is that these data must be copied in different non-continuous columns.</p>
  <p>To give you an example;</p>
  <p>SKU field of the Add product form is in cell D10, in the BD Products sheet the SKU field is in column E.</p>
  <p>CATEGORIA field of the Add product form is in cell H10, in the BD Products sheet the CATEGORIA field is in column D.</p>
  <p>NOMBRE PRODUCTO field of the Add Product form is in cell D12, in the BD Products sheet, the NOMBRE PRODUCTO field is in column C.</p>
  <p>PVP field of the Add product form is in cell E14, in the BD Products sheet the PVP field is in column Z.</p>
  <p>% DESPERDICIO field of the Add product form is in cell D19, in the BD Products sheet the % DESPERDICIO field is in column M.</p>
  <p>And so on continuously.</p>
  <p>As you can see, in the BD Products sheet there are columns in between that do not have fields to fill in in the form of the Add Product sheet because they are columns that contain formulas that make automatic calculations.</p>
  <p>In addition to that, I would like the form data of the &quot;Add Product&quot; sheet to be copied one row after the last occupied row of the BD Products sheet (which I do with the getLasRow () + 1).</p>
  <p>How can you do that? I don't think she's the only one in such a situation ...</p>
  <p>I've been looking for information on how to do it on YouTube, Google, and here on Stackoverflow, but I just can't find information about it.</p>
  <p>It is worth mentioning that I am a total novice and it is my first code.</p>
  <p>The code that I have done so far is the following:</p>
  <pre><code>function Enviar4() {
  
    const libro = SpreadsheetApp.getActiveSpreadsheet();
    const hojaOrigen = libro.getActiveSheet();
    const hojaDestino = libro.getSheetByName('BD Productos');
  
    var NombreProducto = hojaOrigen.getRange('D12').getValue();
    var Categoria = hojaOrigen.getRange('H10').getValue();
    var Sku = hojaOrigen.getRange('D10').getValue();
    var DesperdicioMaterial = hojaOrigen.getRange('D19').getValue();
    var MargenBeneficio = hojaOrigen.getRange('H19').getValue();
    var IvaMayorista = hojaOrigen.getRange('D24').getValue();
    var IvaClienteFinal = hojaOrigen.getRange('H24').getValue();
    var PvpClienteFinal = hojaOrigen.getRange('E14').getValue();
  
  
    var producto = [[NombreProducto,Categoria,Sku,DesperdicioMaterial,MargenBeneficio,IvaMayorista,IvaClienteFinal,PvpClienteFinal]]
  
    const rangoDestino = hojaDestino.getRange(hojaDestino.getLastRow()+1, 1, 1, hojaDestino.getLastColumn());
  
    rangoDestino.setValues(producto);
  
    spreadsheet.getRangeList(['D10', 'H10', 'D12', 'E14', 'D19', 'H19', 'D24', 'H24']).clearContent();
  
  }
  </code></pre>
  <p>I also attach screenshots:</p>
  <p>Add product: <a href="https://drive.google.com/file/d/1oKUGJsgomxNa2mCSn9e6GJq78JmmpIcf/view?usp=sharing" rel="nofollow noreferrer">https://drive.google.com/file/d/1oKUGJsgomxNa2mCSn9e6GJq78JmmpIcf/view?usp=sharing</a></p>
  <p>BD Products: <a href="https://drive.google.com/file/d/1CQjfUQSgpIMhoUbTvfKY4ELMpWOO4Dk5/view?usp=sharing" rel="nofollow noreferrer">https://drive.google.com/file/d/1CQjfUQSgpIMhoUbTvfKY4ELMpWOO4Dk5/view?usp=sharing</a></p>
  <p>Can someone help me with the code please? Thank you in advance.</p>
  <p>EDIT:</p>
  <p>Somehow get to achieve it with the following code.
  Now my question is is there any way to simplify this a bit more?</p>
  <pre><code>function Enviar6() {
  
  var libro = SpreadsheetApp.getActiveSpreadsheet(); 
  
  var hojaOrigen = libro.getSheetByName(&quot;Añadir producto&quot;);
  var hojaDestino = libro.getSheetByName(&quot;BD Productos&quot;);
  
  hojaOrigen.getRange(&quot;D12&quot;).copyTo(hojaDestino.getRange(hojaDestino.getLastRow()+1,3,1,1,), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
  hojaOrigen.getRange(&quot;H10&quot;).copyTo(hojaDestino.getRange(hojaDestino.getLastRow(),4,1,1,), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
  hojaOrigen.getRange(&quot;D10&quot;).copyTo(hojaDestino.getRange(hojaDestino.getLastRow(),5,1,1,), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
  hojaOrigen.getRange(&quot;D19&quot;).copyTo(hojaDestino.getRange(hojaDestino.getLastRow(),13,1,1,), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
  hojaOrigen.getRange(&quot;H19&quot;).copyTo(hojaDestino.getRange(hojaDestino.getLastRow(),15,1,1,), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
  hojaOrigen.getRange(&quot;D24&quot;).copyTo(hojaDestino.getRange(hojaDestino.getLastRow(),18,1,1,), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
  hojaOrigen.getRange(&quot;H24&quot;).copyTo(hojaDestino.getRange(hojaDestino.getLastRow(),23,1,1,), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
  hojaOrigen.getRange(&quot;E14&quot;).copyTo(hojaDestino.getRange(hojaDestino.getLastRow(),26,1,1,), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
  
  //Vaciar las celdas del formulario &quot;Añadir producto&quot; después de envíar los datos a la &quot;BD Productos&quot;
   hojaOrigen.getRangeList(['D10', 'H10', 'D12', 'E14', 'D19', 'H19', 'D24', 'H24']).clearContent();
  
  }
  
  </code></pre>