google-sheetsgoogle-apps-script

Copy specific fields to another sheet


I would like to do this:

  1. From a Google form, some fields are filled in and then written in a Google sheet (SOURCE)

    I attach an image:

    enter image description here

  2. I would like that for each new row created in the sheet (Source), some data is copied to another Google sheet in the first free row

    But only some fields of the sheet (source) and in some specific cells.

    I attach an image: enter image description here

I start from this code, which works, but it copies all the fields.

function copyPaste() {
 var ss = SpreadsheetApp.openById("xxxxx");
 var source = ss.getSheetByName("New_Customers");
 var rangeSource = source.getDataRange();
 var data = rangeSource.getValues();
 var lr = rangeSource.getLastRow();
 var lc = rangeSource.getLastColumn();
 Logger.log(data);


 var sss = SpreadsheetApp.openById("xxxx");
 var target = sss.getSheetByName("Sheet1")
 target.getRange(target.getLastRow()+1,1,lr,lc).setValues(data);
 }

Solution

  • Copy and paste specific columns from different sheets

    In order to filter the different columns that you wanted to copy from one sheet to another. you can try the below code:

    Complete code:

    function copyPaste() {
      var ss = SpreadsheetApp.openById("xxxxx");
      var source = ss.getSheetByName("New_Customers");
      var sss = SpreadsheetApp.openById("xxxx");
      var target = sss.getSheetByName("Sheet1")
      var targetRow = target.getLastRow() + 1;
      
      var values = source.getRange(source.getLastRow(), 1, 1, source.getLastColumn()).getValues();
    
      //getting the values form `New_Customers` sheet
      var bval = values[0][1];
      var cval = values[0][2];
      var eval = values[0][5];
      var gval = values[0][7];
      var dval = values[0][3];
    
      //setting the values to `Sheet1` sheet
      target.getRange(targetRow, 1).setValue(bval);
      target.getRange(targetRow, 2).setValue(cval);
      target.getRange(targetRow, 3).setValue(eval);
      target.getRange(targetRow, 5).setValue(gval);
      target.getRange(targetRow, 11).setValue(dval);
    }
    

    NOTE: this has some limitations when it comes to runtime since you are using multiple getRange() and setValue()

    As an alternative approach for this, you can replace the code below the //setting the values to improve runtime but it will set an empty values to the other columns between them, which is its limitation when you already have values within the empty ranges (e.g. image, and Giomi dall'ultimo compleanno columns of target sheet) before submitting the form:

    Alternative setting the values:

      var values = [[bval, cval, eval, "", gval, "", "", "", "", "", dval]];
      target.getRange(targetRow, 2, 1, 11).setValues(values);  
    

    Please do not forget to add an On Form Submit Installable trigger so that every value submitted will be copied automatically to the other sheets.

    Sample output: enter image description here

    NOTE: The values and ranges are only for the demo purposes on how the code works

    Additional/Some improvements:

    If you only have one spreadsheet for this project, you can easily use the getActiveSpreadsheet() and maximize the getSheetByName() to minimize the runtime issue.

    Instead of:

      var ss = SpreadsheetApp.openById("xxxxx");
      var source = ss.getSheetByName("New_Customers");
      var sss = SpreadsheetApp.openById("xxxx");
      var target = sss.getSheetByName("Sheet1")
    

    You can use:

      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var source = ss.getSheetByName("New_Customers");
      var target = ss.getSheetByName("Sheet1");
    

    References:

    1. getRange(row, column)
    2. getRange(row, column, numRows, numColumns)