2

How do I edit the Apps Script below to only copy over data from Column A to L and P to S? (Basically, I want it to skip Column M, N, O and all the columns after S.

function onEdit(event) {
  // assumes source data in sheet named Tab 1
  // target sheet of move to named Tab 2
  // test column with yes/no is col 20 or T
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = event.source.getActiveSheet();
  var r = event.source.getActiveRange();

  if(s.getName() == "Tab 1" && r.getColumn() == 20 && r.getValue() == "yes") {
    var row = r.getRow();
    var numColumns = s.getLastColumn();
    var targetSheet = ss.getSheetByName("Tab 2");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).moveTo(target);
    s.deleteRow(row);
  }
}

Thank you in advance, Best regards, Robert

Jack871
  • 75
  • 6

1 Answers1

2

Copy both ranges (A-L, P-S) separately to the different target ranges, as shown here:

function onEdit(e) {
  //e.source.toast("Entry");
  //console.log(JSON.stringify(e));
  const sh=e.range.getSheet();
  if(sh.getName()=="Sheet1" && e.range.columnStart==20 && e.value=="TRUE") {
    const tsh=e.source.getSheetByName('Sheet2');
    const nr=tsh.getLastRow()+1;
    sh.getRange(e.range.rowStart,1,1,12).moveTo(tsh.getRange(nr,1,1,12));
    sh.getRange(e.range.rowStart,16,1,4).moveTo(tsh.getRange(nr,16,1,4));
    sh.deleteRow(e.range.rowStart);
  }
}

Note: I used 'Sheet1' and 'Sheet2' and I also used standard values for check boxes instead of 'yes' and 'no'. I also used 'e' instead of 'event' and I used the data that is available in the event object.

Iamblichus
  • 18,540
  • 2
  • 11
  • 27
Cooper
  • 59,616
  • 6
  • 23
  • 54
  • Would you consider adding an explanation of what you did? Something along the lines of "Retrieve and use both source and both target ranges separately". – Iamblichus Oct 12 '20 at 08:27